无法使用 Salesforce Crypto Class for Google Maps API 生成 HMac

编程入门 行业动态 更新时间:2024-10-23 23:28:12
本文介绍了无法使用 Salesforce Crypto Class for Google Maps API 生成 HMac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的公司最近注册了 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/maps/documentation/business/webservices

Here is the link to Google Documentation where you can also find the same example: https://developers.google/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 17:21:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/943653.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Class   Crypto   Salesforce   Google   HMac

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!