Reply
Contributor
Taneja
Posts: 8
0
Accepted Solution

Unable to Generate HMac using SF Crypto Class for Google Maps API

My Company recently signed up for Google Maps API for business.

 

To use the API, I need to generte a HMacSHA1 signature, and add it to my HTTP request. Unfortunately, somehow, I am not able to generate the right signature.

 

For testing, I am using the values provided by google to ensure that the algorithm works fine and I get the right result. Here is the code:

 

string url = 'maps/api/geocode/json?address=New+York&sensor=false&client=clientID';
string privateKey = 'vNIXE0xscrmjlyV-12Nj_BvUPaw=';

privateKey = privateKey.replace('-', '+');
privateKey = privateKey.replace('_', '/');

 

//Blob privateKeyBlob = EncodingUtil.base64Decode(privateKey);
Blob privateKeyBlob = Blob.valueOf(privateKey);
Blob urlBlob = Blob.valueOf(url);

Blob signatureBlob = Crypto.generateMac('HMacSHA1', urlBlob, privateKeyBlob);

 

String signature =EncodingUtil.urlEncode(EncodingUtil.base64Encode(signatureBlob), 'UTF-8');

signature = signature.replace('+', '-');
signature = signature.replace('/', '_');

 

system.debug('signature is ' +signature);

 

The generated signature should be : KrU1TzVQM7Ur0i8i7K3huiw3MsA=

 

Here is the link to Google Documentation where you can also find the same example: https://developers.google.com/maps/documentation/business/webservices

 

Few points to note:

1. I used the sample Python script provided in API Documentation and it gives the right result.

2. I think the problem is, API Documentation says that we should decode the privateKey and then provide it to the function. Although the documentation for Crypto Class says the "The value of privateKey does not need to be in decoded form.". I tried both, with and without decoding, still no result.

3. For Google API, everything has to be UTF-8 Encoded; I don't know if thats the way Encoding.Util decode's it.

 

I have tried a lot fo combinations, but could not find a solution. Any help would be highly appreciated.

 

Thanks,

Ankit

 

 

 

Newbie
Anil Reddy
Posts: 3
0

Re: Unable to Generate HMac using SF Crypto Class for Google Maps API

Hi,

We have the same requirement in our project and i am unable to get the url signed.Can you please post the code to get url signed in apex for geocoding withe google maps API.

 

Thanks in Advance

Contributor
Taneja
Posts: 8
0

Re: Unable to Generate HMac using SF Crypto Class for Google Maps API

Hi Anil, 

 

Here is the code ( This if for google Maps v3 ) :

 

string url = '/maps/api/geocode/json?address=New+York&sensor=false&client=PUT THE CLIENT NAME HERE';

string privateKey = 'PUT THE PRIVATE KEY HERE';
privateKey = privateKey.replace('-', '+');
privateKey = privateKey.replace('_', '/');

Blob privateKeyBlob = EncodingUtil.base64Decode(privateKey);
//Blob privateKeyBlob = Blob.valueOf(privateKey);
Blob urlBlob = Blob.valueOf(url);
Blob signatureBlob = Crypto.generateMac('HMacSHA1', urlBlob, privateKeyBlob);

String signature =EncodingUtil.base64Encode(signatureBlob);
signature = signature.replace('+', '-');
signature = signature.replace('/', '_');

system.debug('signature is ' +signature);

url = 'https://maps.googleapis.com' + url;
url += '&signature=' + signature;

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length', '0');
req.setEndpoint(url);
req.setMethod('POST');
String responseBody;
HttpResponse res;
res = h.send(req);
responseBody = res.getBody();  
system.debug('response ' + responseBody); 

 Let me know if this helps and please Mark it as a solution if it solved your problem.

 

Best,

Ankit

Newbie
Anil Reddy
Posts: 3
0

Re: Unable to Generate HMac using SF Crypto Class for Google Maps API

Thanks Ankit ,This solved my issue.

Contributor
Taneja
Posts: 8
0

Re: Unable to Generate HMac using SF Crypto Class for Google Maps API

Please Mark that as Solution. :)