"OAuth2 is not provided or is not canceled" when trying to evaluate the free trial in the Chrome extension

I am trying to offer a free trial for my Chrome extension and am following the Chrome documentation on how to do this.

When loading my extension, the background script logs the following error on the console:

Disabled runtime.lastError when running identity.getAuthToken: OAuth2 is not provided or is not canceled.

The console points to calling chrome.identity.getAuthToken as the culprit. Here is the corresponding code in my background script:

 var CWS_LICENSE_API_URL = 'https://www.googleapis.com/chromewebstore/v1.1/userlicenses/'; chrome.identity.getAuthToken({ 'interactive': false }, function(token) { console.log('token', token); var req = new XMLHttpRequest(); req.open('GET', CWS_LICENSE_API_URL + chrome.runtime.id); req.setRequestHeader('Authorization', 'Bearer ' + token); req.onreadystatechange = function() { if (req.readyState == 4) { var license = JSON.parse(req.responseText); console.log('license', license); } }; req.send(); }); 

My manifest is set up like this (some snippets are omitted for brevity):

 "manifest_version": 2, "key": "kkkkkkkkkkkkkkk", "background": { "scripts": [ "background.js" ] }, "permissions": [ "storage", "identity", "https://www.googleapis.com/" ], "oauth2": { "client_id": "cccccccccc.apps.googleusercontent.com", "scopes": [ "https://www.googleapis.com/auth/chromewebstore.readonly" ] } 

Here is what I tried or confirmed:

  • The client ID matches the value in the Google Developer Console that was generated using my extension ID.
  • The Chrome Web Store API is enabled in the Google Developer Console (this is the only API available).
  • The manifest key corresponds to the value generated after I put the extension in the web store.
  • A call to getAuthToken with interactive mode enabled results in the same error.
  • I compared my code with in this example , and nothing jumps out at me as significantly different (although an extra pair of eyes to confirm will not “hurt”).

In case that matters, I use Chrome 42.0.2311.135 (64-bit) on Mac OS X.

Any ideas on what causes the error, and what do I need to change to get it gone so that I can find the token and authorization license?

+8
google-chrome google-chrome-extension
source share
1 answer

According to the code, the only change that needs to be made is to enable interactive mode:

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

There were also several PEBCAK questions. Namely:

  • An interactive login page may take a few seconds. This seems to be a bandwidth issue. This may be part of why the documentation suggests initiating an auth request for some kind of user interaction, rather than when the extension is loaded first.
  • Switching between false and true and reloading the extension was not a sufficient test of functionality. The result of getAuthToken cached. When I cancel auth and then update or even remove and re-add the extension, the same token keeps coming back for some time. Restarting Chrome with Interactive Mode enabled is what led me to this decision.
+10
source share

All Articles