Skip to content

Instantly share code, notes, and snippets.

@jkusachi
Last active November 21, 2016 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkusachi/e60bc6d76ce41142426729726a197255 to your computer and use it in GitHub Desktop.
Save jkusachi/e60bc6d76ce41142426729726a197255 to your computer and use it in GitHub Desktop.
Static Google Map Signature
const url = require('url');
const crypto = require('crypto');
const config = {
secret: 'YOUR_SECRET',
clientId: 'CLIENT_ID'
}
const generateSignature = (address) => {
const request = url.parse(address);
// You may need to decode this key into its original binary format
const decodedSecret = Buffer.from(config.secret, 'base64');
const algorithm = crypto.createHmac('sha1', decodedSecret);
const hash = algorithm.update(request.path).digest('base64');
// convert base64 to urlsafe base64
const signature = hash
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
return signature;
}
const requestUrl = `https://maps.googleapis.com/maps/api/staticmap?center=40.714%2c%20-73.998&zoom=12&size=400x400&client=${config.clientId}`;
const signature = generateSignature(requestUrl);
console.log(`${requestUrl}&signature=${signature}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment