Need to run getAuthToken twice before receiving access token, why?

764 views Asked by At

I think I need to take some aspirin... banging my head to the wall... 3 days and still getting nowhere... I just don't get it...

From a Chrome extension I'm calling getAuthToken from background.js, it is probably the most basic script ever written:

function getAuthToken(){            
        chrome.identity.getAuthToken(   
        {'interactive': true},
            function(token){                            
                console.log(token);         
            } 
        );     
};

Manifest snippet:

"content_security_policy": "script-src https://*.google.com 'unsafe-eval'; object-src 'self'",
  "oauth2": {
    "client_id": "<client_id>.apps.googleusercontent.com",
    "scopes": ["https://www.googleapis.com/auth/gmail.modify"]
  } 

This is what happens:

  1. Load the extension
  2. OAuth2 is started, access to GMail is asked (pop-up) and granted
  3. Console log: token = undefined and Unchecked runtime.lastError while running identity.getAuthToken: Authorization page could not be loaded (lastError 133)
  4. In Chrome tokencache I see a new, but not present token (no token string)
  5. Reload the extension
  6. No Gmail access is asked
  7. Token is now correctly received

Why do I need to run getAuthToken twice? I use step 2 authentication for GMail but understood this only applies to the front end.

Please if you ran into something similar share it with me, it is so frustrating not getting any further than 5 lines of code.

0

There are 0 answers