javascript - Oauth2 token response "invalid_grant" with JWT from GoogleAPI -
i have enabled service account google api console perform two-legged oauth
making calls google analytics api. using javascript , have followed pure javascript implementation of jwt , jws generate signed jwt , make http/rest call token described in guide. once make post
encodedurl 400 "error": "invalid_grant". "error_description": "bad request"
i read posts sure include email , not client_id
in request. made sure case , since google has updated documentation.
any ideas? post code if necessary followed rothrock's post @ link.
i tried signed jwt on http://jwt.io/ , says it's invalid signature. payload
, header
decode correctly. there way test signature after encoding private key provided google api console?
here code:
var pheader = {"alg":"rs256", "typ":"jwt"}; var sheader = json.stringify(pheader); var pclaim ={}; pclaim.iss = "(serviceaccount_email_address).apps.googleusercontent.com"; pclaim.scope = "https://www.googleapis.com/auth/analytics.readonly"; pclaim.aud = "htps://www.googleapis.com/oauth2/v3/token"; pclaim.exp = kjur.jws.intdate.get("now + 1hour"); pclaim.iat = kjur.jws.intdate.get("now"); var sclaim = json.stringify(pclaim); var key = "-----begin private key----- private key json file ----end private key-----"; var sjws = kjur.jws.jws.sign('rs256', sheader, sclaim, key); var urlencodeddata = ''; var urlencodeddatapairs = []; var token; urlencodeddatapairs.push("grant_type" + '=' + "urn:ietf:params:oauth:grant-type:jwt-bearer"); urlencodeddatapairs.push("assertion" + '=' + sjws); urlencodeddata = urlencodeddatapairs.join('&').replace(/%20/g,'+'); $(function(){ $.ajax({ type: "post", url: 'https://www.googleapis.com/oauth2/v3/token', data: urlencodeddata, datatype: 'json', contenttype: 'application/x-www-form-urlencoded', success: function(result){ console.log(json.stringify(result)); } }); });
i found answer sometime back, want post here clarification. in code above correct. ssh key provided had \n
chars in valid. key had line between -----begin private key-----
, starting of actual string of key.
Comments
Post a Comment