I am writing code to try to get a token for use by Google in OAuth2. This is for the service account, so the instructions are here:
https://developers.google.com/identity/protocols/OAuth2ServiceAccount
I keep getting this error when I submit the JWT to Google:
{"error": "invalid_grant", "error_description": "Invalid JWT signature." }
Here is the code:
try{ var nowInSeconds : Number = (Date.now() / 1000); nowInSeconds = Math.round(nowInSeconds); var fiftyNineMinutesFromNowInSeconds : Number = nowInSeconds + (59 * 60); var claimSet : Object = {}; claimSet.iss = "{{RemovedForPrivacy}}"; claimSet.scope = "https://www.googleapis.com/auth/plus.business.manage"; claimSet.aud = "https://www.googleapis.com/oauth2/v4/token"; claimSet.iat = nowInSeconds; claimSet.exp = fiftyNineMinutesFromNowInSeconds; var header : Object = {}; header.alg = "RS256"; header.typ = "JWT"; var claimSetString = JSON.stringify(claimSet); var headerString = JSON.stringify(header); var claimSetBaseSixtyFour = StringUtils.encodeBase64(claimSetString); var headerBaseSixtyFour = StringUtils.encodeBase64(headerString); var privateKey = "{{RemovedForPrivacy}}"; var signature : Signature = Signature(); signature = signature.sign(headerBaseSixtyFour + "." + claimSetBaseSixtyFour, privateKey , "SHA256withRSA"); var JWT = headerBaseSixtyFour + "." + claimSetBaseSixtyFour + "." + signature; var grantType = "urn:ietf:params:oauth:grant-type:jwt-bearer" var assertions : String = "grant_type=" + dw.crypto.Encoding.toURI(grantType) + "&assertion=" + dw.crypto.Encoding.toURI(JWT); var httpClient : HTTPClient = new HTTPClient(); httpClient.setRequestHeader("content-type", "application/x-www-form-urlencoded; charset=utf-8"); httpClient.timeout = 30000; httpClient.open('POST', "https://www.googleapis.com/oauth2/v4/token"); httpClient.send(assertions); if (httpClient.statusCode == 200) {
Does anyone know why JWT is not working?
Thank you very much! Brad
source share