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

查看:37
本文介绍了无法使用 Salesforce Crypto Class for Google Maps API 生成 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.

为了测试,我使用谷歌提供的值来确保算法运行良好并且我得到正确的结果.代码如下:

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. 我使用了 API 文档中提供的示例 Python 脚本,它给出了正确的结果.2.我认为问题是,API文档说我们应该解码privateKey然后将其提供给函数.尽管 Crypto Class 的文档说privateKey 的值不需要采用解码形式.".我尝试了解码和不解码,仍然没有结果.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.

谢谢,安吉特

推荐答案

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

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.

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

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