BookmarkSubscribeRSS Feed
rudfaden
Lapis Lazuli | Level 10

I am trying to make an api call to google maps using a HMAC-SHA1 signed key. I have to follow these steps.

 

  1. 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.

  2. 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

  3. 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.

  4. Encode the resulting binary signature using the modified Base64 for URLs to convert this signature into something that can be passed within a URL.

  5. 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=*/

 

2 REPLIES 2
ChrisHemedinger
Community Manager

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.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
rudfaden
Lapis Lazuli | Level 10
I am trying to use a Google premium account for google maps, as I need to use Google maps beyond with is available free

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2041 views
  • 0 likes
  • 2 in conversation