chrome.identity.getAuthToken not working

2020-05-20 08:26发布

问题:

I am working with Chrome Identity API, to provide users with a Google Authentication on my Chrome Extension.

I followed the official tutorial of Google : link Chrome Identity API

  • i added permissions to manifest.json and Client ID and Scopes
  • to trigger the Authentification i added this code in my Background.js

chrome.identity.getAuthToken

The problem is when i execute the extension i am redirected to this login page

chrome://chrome-signin/?source=5

but after a successfull authentication i am redirected another time to the login page

what is the problem ?

Manifest.json

{
  "name": "My Extension",
  "short_name": "Ex App",
  "version": "0.1",
  "description": "description",
  "manifest_version": 2,
  "icons": { "128": "ICONE.png", "48": "ICONE1.png" },
  "permissions": ["contextMenus", "identity", "cookies", "http://*/*",   "https://*/*", "<all_urls>", "unlimitedStorage"],
  "browser_action": {
    "default_title": "Title",
    "default_icon": "imgIcone.png"
  },
  "oauth2": {
        "client_id": "xxxxxxx",
        "scopes": [
            "https://www.googleapis.com/auth/userinfo.email"
    ]   
},   

  "background": {
    "scripts": ["background.js"]
  },

    "content_scripts"   : [
{
  "matches": ["*://*.google.com/*"],
  "js": ["gadget/js/jquery.js","gadget/js/contactcard.js"],
  "css": ["gadget/css/contactcard.css"],
  "all_frames": true
  }],  

  "content_security_policy": "script-src 'self' 'unsafe-eval' https://apis.google.com/; object-src 'self'",
  "web_accessible_resources": ["img.png","gadget/css/contactcard.css","gadget/img/extension/crec.png"]
}

Background.js:

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

回答1:

Problem Resolved !

the app ID of my Chrome extension does not match with my app ID in Google Console Developpers To avoid this upload first your Extension then copy the ID from Chrome and set it in the Google Console Developpers ! you must update it in Google console everytime you change it in Google Chrome



回答2:

The accepted answer is incorrect. You can upload it to the webstore, publish it, then install it from the webstore. After installing, find the installed extension app dir (~/Library/Application\ Support/Google/Chrome/Default/Extensions/<ext id> on a mac), and copy the key key from the installed manifest.json into your source code manifest.json. After adding the key field to your manifest.json, your app id will not change during local development.

Source: https://developer.chrome.com/apps/app_identity#copy_key



回答3:

For other user, who might face the problem that chrome.identify.getauthtoken is not firing or you log in and nothing happens.

Check your manifest and reduce it to the bare minimum. For example the "oauth2": { "client.id": "alongstring" , "scopes": ["bla","openid", "bla"]}. Having openid in the array, the callback simply did not fire, no alert, no nothing.

Having openid inside my scopes inside oauth2 blocked in someway the callback from being fired.



回答4:

If the callback in chrome.identity.getAuthToken is not firing at all for you, make sure you are using "browser_action" in your manifest.json and not "page_action".