chrome.identity.getAuthToken 不工作

chrome.identity.getAuthToken not working

我正在使用 Chrome 身份 API,为用户提供 Google 我的 Chrome 扩展的身份验证。

我按照Google的官方教程做了:link Chrome Identity API

chrome.identity.getAuthToken

问题是当我执行扩展程序时,我被重定向到这个登录页面

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

但在成功验证后,我又一次被重定向到登录页面

有什么问题?

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); 
            });

问题已解决!

我的 Chrome extension 应用 ID Google Console Developpers 中的 应用 ID 不匹配 为避免此上传,首先您的扩展程序然后从 Chrome 复制 ID 并将其设置在 Google 控制台开发人员中! 每次在 Google Chrome

中更改它时,都必须在 Google 控制台中更新它

接受的答案不正确。您可以将它上传到网上商店,发布它,然后从网上商店安装它。安装后,找到已安装的扩展应用程序目录(~/Library/Application\ Support/Google/Chrome/Default/Extensions/<ext id> on a mac),并将已安装的 manifest.json 中的 key 密钥复制到您的源代码 manifest.json .将key字段添加到您的manifest.json后,您的应用程序ID在本地开发期间将不会更改。

来源:https://developer.chrome.com/apps/app_identity#copy_key

对于其他用户,他们可能会遇到 chrome.identify.getauthtoken 未触发或您登录但没有任何反应的问题。

检查您的清单并将其减少到最低限度。例如 "oauth2":{ "client.id":"alongstring","scopes":["bla","openid","bla"]}。在数组中有 openid,回调根本没有触发,没有警报,什么都没有。

在我的 oauth2 范围内设置 openid 以某种方式阻止回调被触发。

如果 chrome.identity.getAuthToken 中的回调根本没有为您触发,请确保您在 manifest.json 中使用 "browser_action" 而不是 "page_action"。