Chrome扩展程序gmail API cookiePolicy?

Mar*_*ode 9 javascript google-api google-chrome-extension

我正在构建一个chrome扩展程序,它将读取用户的电子邮件并检查它们是否存在拼写错误.但是,当我尝试在我的background.js中验证用户时,我遇到了这个错误:

uO {message:"无效的cookiePolicy",堆栈:"gapi.auth2.ExternallyVisibleError:无效的cookieP ...在handleResponse(extensions :: sendRequest:67:7)"}

这是我试图验证它们的方式:

background.js

var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://apis.google.com/js/client.js?onload=callbackFunction";
head.appendChild(script);

chrome.identity.getAuthToken({interactive: true}, authorize);

function authorize(token) {
    gapi.auth.authorize({
        client_id: '800382879116-k3luktdc1lmb1e1fml8i8u.apps.googleusercontent.com',
        immediate: true,
        scope: 'https://www.googleapis.com/auth/gmail.readonly'
    },
    function(result){
        console.log(result);
        gapi.client.load('gmail', 'v1', callback);
    });
}
Run Code Online (Sandbox Code Playgroud)

background.html

<!DOCTYPE html>
<html>
    <body>
        <script src='scripts/background.js'></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

的manifest.json

  {
    "name": "Gmail Typo Analyzer",
    "version": "0.1",
    "description": "Gmail Typo Analyzer",
    "permissions": [
      "identity",
      "storage"
    ],
    "content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'",
    "oauth2": {
      "client_id": "82879116-k3luktdc1li8u.apps.googleusercontent.com",
      "scopes": [
          "https://www.googleapis.com/auth/gmail.readonly",
          "https://www.googleapis.com/auth/userinfo.email"
      ]
    },
    "browser_action": {
      "default_popup": "popup.html",
      "default_icon": "images/Icon_16.png"
    },
    "background": {
      "page": "background.html",
      "persistent": false
    },
    "icons": {
      "16": "images/Icon_16.png",
      "32": "images/Icon_32.png",
      "48": "images/Icon_48.png",
      "128": "images/Icon_128.png"
    },
    "manifest_version": 2,
    "key": "c0Kn5f+t92r4P8lmmoDlKtQ6X9Q42UfFtkkiSRBAVMPHnIHqOQvYC67VczJefSNTGpUYa8+wQDFoFj/clH9SfR+BvOGgI6BUVKBNGGoFS"
  }
Run Code Online (Sandbox Code Playgroud)

我现在非常失落,因为他们似乎并不是实现我在任何地方所做的事情的明确指南.有谁知道我可能做错了什么?

Ivá*_*oko 4

您没有发布您的manifest.json文件,您可以在其中设置oauth2凭据,所以我会尝试类似的操作:

清单.json

...
"oauth2" : "client_id": "800382879116-k3luktdc1lmb1e1fml8i8u.apps.googleusercontent.com",
           "scopes": [
               "https://www.googleapis.com/auth/gmail.readonly"
           ]
...
Run Code Online (Sandbox Code Playgroud)

背景.js

chrome.identity.getAuthToken({interactive: true}, authorize);

function authorize(token) {
    if (token) {
         //user has given authorization, use token for requests.
         //...
    } else {
         //no authorization received.
         console.log('No authorization. Error: ' + chrome.runtime.lastError);
    }
};
Run Code Online (Sandbox Code Playgroud)

并且不需要加载 Google API 客户端,就可以通过s 或Fetch API访问Gmail 的 Restful APIXMLHttpRequest