Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Unable to Generate HMac using SF Crypto Class for ...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Unable to Generate HMac using SF Crypto Class for Google Maps API
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 04:55 AM
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=fal
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(
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/b
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
Solved! Go to Solution.
Re: Unable to Generate HMac using SF Crypto Class for Google Maps API
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
04-29-2013 02:22 AM
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
Re: Unable to Generate HMac using SF Crypto Class for Google Maps API
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
04-30-2013 01:01 AM
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
Re: Unable to Generate HMac using SF Crypto Class for Google Maps API
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
04-30-2013 02:26 AM
Thanks Ankit ,This solved my issue.
Re: Unable to Generate HMac using SF Crypto Class for Google Maps API
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
04-30-2013 04:28 AM

