I am trying to make an api call to google maps using a HMAC-SHA1 signed key. I have to follow these steps.
Construct the request URL without the signature, making sure to include your client
parameter. Note that any non-standard characters will need to be URL-encoded:
https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&client=clientID
Note: All Google services require UTF-8 character encoding (which implicitly includes ASCII). If your applications operate using other character sets, make sure they construct URLs using UTF-8 and properly URL-encode them.
Strip off the domain portion of the request, leaving only the path and the query:
/maps/api/directions/json?origin=Toronto&destination=Montreal&client=clientID
Retrieve your private key, which is encoded in a modified Base64 for URLs, and sign the URL above using the HMAC-SHA1 algorithm. You may need to decode this key into its original binary format. Note that in most cryptographic libraries, the resulting signature will be in binary format.
Note: Modified Base64 for URLs replaces the +
and /
characters of standard Base64 with -
and _
respectively, so that these Base64 signatures no longer need to be URL-encoded.
Encode the resulting binary signature using the modified Base64 for URLs to convert this signature into something that can be passed within a URL.
Attach this signature to the URL within a signature
parameter:
https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&client=clientID&signature=base64signature
I found this code online, which i thought would help. But it seems to give the wrong result. I have used test case from googles website. The key suppose to be chaRF2hTJKOScPr-RQCEhZbSzIE=, but the key returned is MqYEMSmWU6nGbkqbN8DBP6PQC2o=
%let key = %nrstr(vNIXE0xscrmjlyV-12Nj_BvUPaw=); %let message = %sysfunc(urlencode(%nrstr(https://maps.googleapis.com/maps/api/geocode/json?address=New+York&client=clientID))); proc groovy; add sasjar="commons_codec" version="1.7.0.0_SAS_20121211183158"; *version is specific to SAS Installation and may differ from this; submit "&key." "&message."; import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec import org.apache.commons.codec.binary.Base64 def base64hmacsha1(key, message) { mac = Mac.getInstance("HmacSHA1") mac.init(new SecretKeySpec(key.getBytes(), "HmacSHA1")) sha1_bytes = mac.doFinal(message.getBytes()) base64 = new Base64() return new String(base64.encode(sha1_bytes)) } exports.base64hmacsha1 = base64hmacsha1(args[0], args[1]) endsubmit; quit; %put &base64hmacsha1.; /*chaRF2hTJKOScPr-RQCEhZbSzIE=*/
Not avoiding your original question, but depending on what you're trying to do with Google Maps, this series of articles about Google Maps with SAS from @LeonidBatkhan might help.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.