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

空扰寡人 提交于 2019-12-06 05:49:46

You need to sign the full path and query. Your string url is missing the leading slash (/).

Also, don't be afraid to open a support case with Google for this type of query.

We have used the code, but there are some defects in it, for whomever intend to use this code, the actual code will be:

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 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: ' + signature);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!