apps-script-oauth2: Too many redirects when attempting authorisation from the Gmail app

I’m working on a gmail add-on with the following code:

    function getGoogleService() {
      return OAuth2.createService('google')
        .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
        .setTokenUrl('https://accounts.google.com/o/oauth2/token')
        .setClientId('...')
        .setClientSecret('...')
        .setCallbackFunction('authCallback')
        .setPropertyStore(PropertiesService.getUserProperties())
        .setScope('profile email')
        .setParam('login_hint', Session.getActiveUser().getEmail())
    }

    function authCallback(callbackRequest) {
      var authorized = getGoogleService().handleCallback(callbackRequest);
  
      if (authorized) {
        return HtmlService.createHtmlOutput(
          'Success! <script>setTimeout(function() { top.window.close() }, 1);</script>'
        );
      } else {
        return HtmlService.createHtmlOutput('Denied');
      }
    }

I’ve taken it from README.md and it works when the authorisation is done from gmail’s desktop web. But when attempting the same from the android Gmail app, the browser shows a TOO_MANY_REDIRECTS error on the https://script.google.com/macros/d/{SCRIPT ID}/usercallback URL, as can be seen on the screenshot.

screenshot_20171115-125749

If I log in first through the web, the add-on works fine on the app. I’ve tried trello and asana add-ons and the same happens with them, so the issue might not be related to this library.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 16 (6 by maintainers)

Most upvoted comments

I believe the way to replicate this is if you are not the 0th currently-logged-in user.

That is to say, if you’re looking at your inbox, and it says https://mail.google.com/mail/u/1/... this will repro.

if it says https://mail.google.com/mail/u/0/... it will not.

I’ve tested this by:

  • Logging in with just Account A as /u/0 - Asana and Dropbox addon’s oauth2 work.
  • Logging in with Account B as /u/0 and Account A as /u/1 - this time, Account B works just fine, and Account A fails with the redirect error for both apps.

Is there some larger ticket that’s tracking this error on the Google side?

Apparently, it is a known issue when users are logged into multiple accounts and the Gmail team is actively working on it.