Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps Google静态地图API:JavaScript中的URL签名_Google Maps_Google Static Maps - Fatal编程技术网

Google maps Google静态地图API:JavaScript中的URL签名

Google maps Google静态地图API:JavaScript中的URL签名,google-maps,google-static-maps,Google Maps,Google Static Maps,如何在JavaScript中生成有效签名,我确实有一个业务密钥和客户端id,但无法继续使用静态映射api有效url签名,因为我正在使用JavaScript调用此api,并希望发送签名和客户端id以充分利用我的使用限制 谷歌不建议使用JavaScript对URL进行签名,因为它会向用户公开加密密钥 您可以在此链接中看到: 如果您的意思是您有一个Javascript服务器(NodeJS/Meteor)并且想要获得签名的URL,那么我使用这个包 这里有一个指向示例Javascript(node.js)

如何在JavaScript中生成有效签名,我确实有一个业务密钥和客户端id,但无法继续使用静态映射api有效url签名,因为我正在使用JavaScript调用此api,并希望发送签名和客户端id以充分利用我的使用限制

谷歌不建议使用JavaScript对URL进行签名,因为它会向用户公开加密密钥

您可以在此链接中看到:


如果您的意思是您有一个Javascript服务器(NodeJS/Meteor)并且想要获得签名的URL,那么我使用这个包


这里有一个指向示例Javascript(node.js)代码的链接,Google提供了该代码,用于在其

我已经把这段代码转换成了node/npm模块。以下是如何使用它与gmaps URL签名者库对静态地图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));
包括以下语言的示例,包括Node/JS:

  • 蟒蛇
  • 爪哇
  • Node/JS
  • PHP
  • #
  • Perl
  • 红宝石
  • VB.NET
  • 目标-C
对于Node/JS,它如下所示:

'use strict'

const crypto = require('crypto');
const url = require('url');

/**
 * Convert from 'web safe' base64 to true base64.
 *
 * @param  {string} safeEncodedString The code you want to translate
 *                                    from a web safe form.
 * @return {string}
 */
function removeWebSafe(safeEncodedString) {
  return safeEncodedString.replace(/-/g, '+').replace(/_/g, '/');
}

/**
 * Convert from true base64 to 'web safe' base64
 *
 * @param  {string} encodedString The code you want to translate to a
 *                                web safe form.
 * @return {string}
 */
function makeWebSafe(encodedString) {
  return encodedString.replace(/\+/g, '-').replace(/\//g, '_');
}

/**
 * Takes a base64 code and decodes it.
 *
 * @param  {string} code The encoded data.
 * @return {string}
 */
function decodeBase64Hash(code) {
  // "new Buffer(...)" is deprecated. Use Buffer.from if it exists.
  return Buffer.from ? Buffer.from(code, 'base64') : new Buffer(code, 'base64');
}

/**
 * Takes a key and signs the data with it.
 *
 * @param  {string} key  Your unique secret key.
 * @param  {string} data The url to sign.
 * @return {string}
 */
function encodeBase64Hash(key, data) {
  return crypto.createHmac('sha1', key).update(data).digest('base64');
}

/**
 * Sign a URL using a secret key.
 *
 * @param  {string} path   The url you want to sign.
 * @param  {string} secret Your unique secret key.
 * @return {string}
 */
function sign(path, secret) {
  const uri = url.parse(path);
  const safeSecret = decodeBase64Hash(removeWebSafe(secret));
  const hashedSignature = makeWebSafe(encodeBase64Hash(safeSecret, uri.path));
  return url.format(uri) + '&signature=' + hashedSignature;
}

你解决过这个问题吗?