Thursday, July 17, 2014

Writing sign for URL ...write signature for Google Geocoding API

I have chance to work with Google Geocoding API lately...in Salesforce of course.

The requirement is to retrieve address from Google Geocoding by passing postal code.When passing postal code through HttpRequest, it should return the response in JSON.But I am not going to write how I call it in this entry.I will save it for future.*cough*
So I will write on how to generate signature...
Note on this limitation between free API and paid one.

Users of the free API:
  • 2,500 requests per 24 hour period.
  • 10 requests per second.
Maps for Business customers:
  • 100,000 requests per 24 hour period.
  • 10 requests per second.
To create API you can go here .Use our own Google account.But I found out without API key also it works.

See!!This is free...you can play around with it.
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:97510

So I go for Maps for Business customers, what is that mean?
It means , we required to have client and signature.

Google will give us client and private key.But it does not mean that we are on now.We need generate the signature using private key given.Each time when we request for postal code, we need to generate signature.
Example, postal code different so signature also can be different.

https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:11900&client=gme-acme&signature=signature1

https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:11800&client=gme-acme&signature=signature2

Actually it sound like a lot of job to generate signature.It is not fun because only Phyton and Java examples that available.I need to generate signature before calling the webservice.Calling out itself have limitation in Salesforce.*sigh*

But I after done some research, do experiment ,asking around ...at last I managed to generate the signature in Apex class.One of the forum that inspired me is here . But it seem that I need to play around with replace method  before execute urlEncode.

Anyway, here the code.Note for this we need to use hmacSHA1 algorithm.
You can go ahead and run in Apex Execute Anonymous, but make sure that you have the client and private key.Hope it will help you.I could not guarantee it is error free, but it works for me.Let me know if you have better solution.

*Because this is for corporate use, go ahead and ask Google support.

//replace the number with any postal code to test
String baseUrl = 'https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:97005&client=gme-acme';
URL url = new URL(baseURL);
String path =url.getPath();
String query=url.getQuery();
String input=path+'?'+query;

String privateKey='whateverkeythatyougot';

Blob decodePK = EncodingUtil.base64Decode(privateKey);
String algorithmName = 'hmacSHA1';
Blob hmacData = Crypto.generateMac(algorithmName, Blob.valueOf(input), decodePK);

String encodePK =EncodingUtil.base64Encode(hmacData);
//please make sure to replace.
encodePK = encodePK.replace('+', '-');
encodePK = encodePK.replace('/', '_');
String signature =EncodingUtil.urlEncode(encodePK, 'UTF-8');
signature = signature.replace('+', '-');
signature = signature.replace('/', '_');

baseUrl +='&signature='+signature;

//this is the final URL that  can be pasted to browser
system.debug('@unid baseURL ='+ baseURL);

References:
Google Geocoding API
Digital Signature
Stack Overflow
Salesforce Crypto Class