Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.