I'm attempting to offer a free trial period for my Chrome extension and have been following the Chrome documentation about how this can be accomplished.
When my extension loads, though, the background script is logging the following error to the console:
Unchecked runtime.lastError while running identity.getAuthToken: OAuth2 not granted or revoked.
The console is pointing at the call to chrome.identity.getAuthToken
as the culprit. Here's the relevant 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 setup like so (some pieces 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's what I've tried or confirmed:
getAuthToken
with interactive mode enabled results in the same error.In case it matters, I'm using Chrome 42.0.2311.135 (64-bit) on Mac OS X.
Any ideas about what is causing the error and what I need to change to make it go away so I can lookup the auth token and license?
Code-wise, the only change needed is to enable interactive mode:
chrome.identity.getAuthToken({
'interactive': true
}, function(token) {
...
});
There were also a couple of PEBCAK issues going on. Namely:
false
and true
and reloading the extension was not a sufficient test of functionality. The result of getAuthToken
is cached. When I revoke the auth and then refresh or even delete and re-add my extension the same token continues to be returned for some amount of time. Restarting Chrome with interactive mode enabled is what got me to this solution.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With