Generating Signature for Google Work API using node.js -


i need generate signature using google map work api ( client id , key inputs) using node.js. can see code in java,ruby not in node.js. can assist here.

thanks siva.

google provides nice howto technical details. javascript sample missing though. code listed below adapted c# version (i put appropriate comments there).

'use strict';  const url = require('url'); const crypto = require('crypto');  const sign = (apiurl, keystring) => {     // converting key bytes throw exception, need replace '-' , '_' characters first.     const usableprivatekey = keystring.replace(/[-]/g, '+').replace(/[_]/g, '/');      const privatekeybytes = new buffer(usableprivatekey, 'base64');     const uri = url.parse(apiurl);      // compute hash     const algorithm = crypto.createhmac('sha1', privatekeybytes);     const hash = algorithm.update(uri.path).digest('base64');      // convert bytes string , make url-safe replacing '+' , '/' characters     const signature = hash.replace(/[+]/g, '-').replace(/[/]/g, '_');      // add signature existing uri     return uri.protocol + '//' + uri.host + uri.path + '&signature=' + signature; };  module.exports = { sign }; 

Comments