如何在 JavaScript 中生成有效签名,我确实有业务 key 和客户端 ID,但无法继续处理静态 map api 有效的 url 签名,因为我正在使用 JavaScript 调用此 API 并且想要发送签名和客户端 ID 以充分利用我的使用限制。
最佳答案
这里是 Google 提供的示例 Javascript (node.js) 代码的链接,用于在其 documentation 中签署静态 map URL :
https://github.com/googlemaps/url-signing/blob/gh-pages/urlSigner.js
我已经将这段代码变成了 gmaps-url-signer节点/npm 模块。您可以通过以下方式使用 gmaps-url-signer 库对静态 map URL 进行签名:
const urlSigner = require('gmaps-url-signer');
const key = 'my_google_api_key';
const secret = 'my_static_maps_secret';
const domain = 'http://maps.googleapis.com';
// Path to your static map
let path = '/maps/api/staticmap?zoom=2&scale=1&size=350x250&maptype=terrain&format=png&visual_refresh=true';
path += `&key=${key}`;
console.log('Full signed URL:');
console.log(domain + urlSigner.sign(path, secret));
关于google-maps - 谷歌静态 map API : URL Signature in JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15259885/