Last active
November 21, 2016 18:19
-
-
Save jkusachi/e60bc6d76ce41142426729726a197255 to your computer and use it in GitHub Desktop.
Static Google Map Signature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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