如何在ruby中使用SHA-1 HMAC为google地图生成签名

如何在ruby中使用SHA-1 HMAC为google地图生成签名,ruby,hmacsha1,Ruby,Hmacsha1,我正在尝试使用ruby中的SHA-1 HMAC为GoogleMaps调用生成签名。我从互联网上获得了一个python代码,我正试图将其复制到ruby中。下面是phython的代码 import urllib.parse import base64 import hashlib import hmac GOOGLEAPIS_URL = 'http://maps.googleapis.com' STREETVIEW_ENDPOINT = '/maps/api/streetview' encoded

我正在尝试使用ruby中的SHA-1 HMAC为GoogleMaps调用生成签名。我从互联网上获得了一个python代码,我正试图将其复制到ruby中。下面是phython的代码

import urllib.parse
import base64
import hashlib
import hmac 
GOOGLEAPIS_URL = 'http://maps.googleapis.com'
STREETVIEW_ENDPOINT = '/maps/api/streetview'
encodedParams = urllib.parse.urlencode({'size':'50x50','location':'42.35878993272469,-71.05793081223965','sensor':'false','client':'gme-securealert'});
privateKey = 'Encoded_Key' # e.g XEL-B9Zs3lRLajIXkD-bqTYix20=
decodedKey = base64.urlsafe_b64decode(privateKey)
urlToSign = STREETVIEW_ENDPOINT + '?' + encodedParams
print(urlToSign)
signature = hmac.new(decodedKey, urlToSign.encode('utf-8') , hashlib.sha1)
encodedSignature = base64.urlsafe_b64encode(signature.digest())
print(encodedSignature
)

生成OI2DXDLq7Qd790Lokaxgqtis\u pE=签名。 下面是我试图实现相同签名的Ruby代码

GOOGLE_APIS_URL= "http://maps.googleapis.com"      
key = Encoded_Key # e.g XEL-B9Zs3lRLajIXkD-bqTYix20=
data ='/maps/api/streetview?size=50x50&sensor=false&client=gme-securealert&location=42.35878993272469,-71.05793081223965'

data_array = data.split("?")
STREET_VIEW_ENDPOINT = data_array[0]
query_string = data_array[1]

encoded_query_string =  URI.escape(query_string) # to encode parameters only
decoded_key = Base64.decode64(key) # to decode the key

data = STREET_VIEW_ENDPOINT << '?' << encoded_query_string
#p "DATA #{data}"
#data = Base64.decode64(data)
#puts "data #{data}"
digest = OpenSSL::Digest.new('sha1')
p OpenSSL::HMAC.digest(digest, decoded_key, data)
hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, decoded_key, data))
p hmac
GOOGLE\u api\u URL=”http://maps.googleapis.com"      
密钥=编码密钥#例如XEL-B9Zs3lRLajIXkD-bqTYix20=
数据='/maps/api/streetview?尺寸=50x50,传感器=false,客户端=gme securealert&location=42.35878993272469,-71.05793081223965'
data\u数组=data.split(“?”)
街道\视图\端点=数据\数组[0]
查询字符串=数据数组[1]
encoded_query_string=URI.escape(query_string)#仅对参数进行编码
decoded_key=Base64.decode64(key)#解码密钥

data=STREET\u VIEW\u ENDPOINT您可以为请求参数创建哈希,并使用URI.encode\u www\u表单对参数进行编码。使用Base64.urlsafe_decode64和Base64.urlsafe_encode64,而不是Base64.decode64和Base64.encode64。在我的例子中,我使用了反向地理编码api。您将需要对参数进行加密,并反转\u地理编码\u端点\u JSON。希望这对你有帮助。如果您有任何疑问,请告诉我

GOOGLE_MAPS_API_CLIENT_ID = 'gme-xyz'
GOOGLE_MAPS_API_CRYPTO_KEY = 'private_key'
GOOGLEAPIS_HTTP_URL = 'http://maps.googleapis.com'
REVERSE_GEOCODING_ENDPOINT_JSON = '/maps/api/geocode/json'

str_latlng = lat.to_s + ',' + lon.to_s
encoded_params = URI.encode_www_form({'latlng' => str_latlng,
                                      'client' => GOOGLE_MAPS_API_CLIENT_ID})
decoded_key = Base64.urlsafe_decode64(GOOGLE_MAPS_API_CRYPTO_KEY)
url_to_sign = REVERSE_GEOCODING_ENDPOINT_JSON + '?' + encoded_params
digest = OpenSSL::Digest.new('sha1')
signature = OpenSSL::HMAC.digest(digest, decoded_key, url_to_sign)
encoded_signature = Base64.urlsafe_encode64(signature)
signed_url = GOOGLEAPIS_HTTP_URL + REVERSE_GEOCODING_ENDPOINT_JSON + '?' + encoded_params + '&signature='+encoded_signature

这段代码真的很难阅读,如果你添加一些注释并留出一些空间,它会帮助很多人。