无法使用适用于Google Maps API的Salesforce Crypto Class生成HMac [英] Unable to Generate HMac using Salesforce Crypto Class for Google Maps API

查看:161
本文介绍了无法使用适用于Google Maps API的Salesforce Crypto Class生成HMac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司最近注册了Google Maps API for business。

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

要使用API​​,我需要生成一个HMacSHA1签名,并将其添加到我的HTTP请求中。不幸的是,我无法生成正确的签名。

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.

测试时,我使用google提供的值来确保算法正常工作,并且得到正确的结果。这里是代码:

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);

生成的签名应该是:KrU1TzVQM7Ur0i8i7K3huiw3MsA =

The generated signature should be : KrU1TzVQM7Ur0i8i7K3huiw3MsA=

以下是Google文档的链接,您可以在其中找到相同的示例: https:/ /developers.google.com/maps/documentation/business/webservices

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

几点注意事项:
1.我使用了示例Python脚本在API文档中提供,它给出了正确的结果。
2.我认为问题是,API文档说我们应该解码privateKey然后提供给函数。虽然Crypto Class的文档说私钥的价值不需要以解码形式。我尝试了两种方式,有和没有解码,仍然没有结果。
3.对于Google API,一切都必须是UTF-8编码;我不知道这是Encoding.Util解码的方式。

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.

谢谢,
Ankit

Thanks, Ankit

推荐答案

您需要签署完整路径并进行查询。您的字符串网址缺少前导斜杠(/).

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

另外,不要害怕为Google此类查询打开支持案例。

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

这篇关于无法使用适用于Google Maps API的Salesforce Crypto Class生成HMac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆