Chrome扩展程序gmail API cookiePolicy?

时间:2018-11-14 22:16:00

标签: javascript google-chrome-extension google-api

我正在构建一个chrome扩展程序,该扩展程序将读取用户的电子邮件并检查其输入错误。但是,当尝试在我的background.js中对用户进行身份验证时,我遇到了此错误:

  

uO {消息:“无效的cookiePolicy”,堆栈:   “ gapi.auth2.ExternallyVisibleError:无效的cookieP…在handleResponse   (扩展名:: 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);
    });
}

background.html

<!DOCTYPE html>
<html>
    <body>
        <script src='scripts/background.js'></script>
    </body>
</html>

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"
  }

我现在迷失了,因为他们似乎并没有成为实现我在任何地方都想做的权威指南。有人知道我可能做错了吗?

2 个答案:

答案 0 :(得分:4)

您没有发布oauth2 credentialsmanifest.json文件,因此我会尝试以下操作:

manifest.json

...
"oauth2" : "client_id": "800382879116-k3luktdc1lmb1e1fml8i8u.apps.googleusercontent.com",
           "scopes": [
               "https://www.googleapis.com/auth/gmail.readonly"
           ]
...

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

您无需加载Google API客户端,可以使用XMLHttpRequestGmail's Restful API访问Fetch API

答案 1 :(得分:0)

我也曾经遇到过Cookie错误,但是对于Chrome扩展程序,我只知道如何从“扩展程序”标签中解压缩后加载它们。因此,直接使用gapi对我没用。

如Ivan所述,Chrome扩展程序通过在清单中设置“ oauth2”部分来“内置”此支持。然后,您可以像Ivan的示例一样直接使用Ajax调用API。

以Ivan的示例为基础,这是我的工作代码,用于获取联系人列表。我还没有读取XHR对象,但是Fiddler确认数据可以正常返回,没有任何Cookie或CORS错误。当然,请确保您在console.developers.google.com中启用了这些API。

WorkItemsApi.deleteWorkItem(workItemId, oAuthTwoLegged, oAuthTwoLegged.getCredentials());