4

Context: GMail Add-on code with Library resource OAuth2 at version 29.

I have this code working (adjusted to show here):

function getOAuthService() {
    return OAuth2.createService('myName')
        .setAuthorizationBaseUrl(baseUrl)
        .setTokenUrl(tokenUrl)
        .setTokenFormat('application/x-www-form-urlencoded')
        .setClientId('myId')
        .setClientSecret(mySecret)
        .setScope('myScope') // arbitrary text
        .setCallbackFunction('authCallback')
        .setCache(CacheService.getUserCache())
        .setPropertyStore(PropertiesService.getUserProperties());
}

The callback function is like this:

function authCallback(request) {
    var service = getOAuthService();
    var authorized = service.handleCallback(request);
    console.log(authorized); // -->   true
    console.log(service.getAccessToken()); // --> undefined
    console.log(JSON.stringify(service.getToken())); // --> see below
    console.log(service.getToken()); // --> see below
}   

The result of the stringified getToken() call is a mess:

{"{\"access_token\":\"IqINbsrIchvc2bV27smQfAy5ldn6s6iJ9z9LPrUx/pJu+j9yJhb1MID/WBeVKrlvzvUrTPdcHdyhYz9CX0WcxLxbtCBU8wk6LdYQ8rRV5/XBE4XsU5Ik4wQNiItOimQf2f1V3VVuNSP/n50LCqVmQG0pNv/d5dUWnvq1OvSSJX9CvyY8RHioCsSn8pt/PTE4GYWy8R0/9wR2HbmIqjaDgg":"","granted_time":1534261033}

and the non-stringified output looks like

{granted_time=1.534261033E9, {"access_token":"IqINbsrIchvc2bV27smQfAy5ldn6s6iJ9z9LPrUx/pJu+j9yJhb1MID/WBeVKrlvzvUrTPdcHdyhYz9CX0WcxLxbtCBU8wk6LdYQ8rRV5/XBE4XsU5Ik4wQNiItOimQf2f1V3VVuNSP/n50LCqVmQG0pNv/d5dUWnvq1OvSSJX9CvyY8RHioCsSn8pt/PTE4GYWy8R0/9wR2HbmIqjaDgg=}

The remote authorization service is returning this JSON when it is called:

{
  "access_token": "IqINbsrIchvc2bV27smQfP/PFXNLCvkfwNRFXoBX4QYH+WojfWNTi1GUdxUKWdaYPbblrAHQnAHgWRSb0iGn1pNIMQVwOYZb6KMY3eZCnxjHwC5lkotEmHdnLX5A2DihiAqaZy3w8ROc0pXafvl+7+E/meWWCnQdKXVEoc/7S3DD49yK0sNqTcA9AOdFrmxB4+SA3d56O6Zh6oOIx4NGug==",
  "token_type": "bearer",
  "expires_in": 3600
}

How do I get this to work correctly and give back the access token?

Glen Little
  • 6,951
  • 4
  • 46
  • 68
  • Why have you decided that token that you get is bad? What's exactly you think is incorrect in it? – dvelopp Aug 20 '18 at 13:57
  • The 'token' that I get is mal-formed JSON with no closing quote on the 'access_token' and no quotes around the 'granted_time' name. I haven't considered just patching that up when I get it, since this is supposed to be dealt with by the library. – Glen Little Aug 20 '18 at 14:04
  • Glen, this seems to be a severe issue with the library. Have you logged it as a bug in the issue tracker ? – hhsb Aug 21 '18 at 06:55
  • Thanks, @HariBalaji. Which issue tracker would be best for code at https://script.google.com/macros/ ? – Glen Little Aug 21 '18 at 12:14
  • Glen, use this "Public Trackers > G Suite Developers > Gmail > Gmail Add-ons" in component while logging an issue. – hhsb Aug 21 '18 at 12:32

0 Answers0