【问题标题】:Using Chrome auth to access gmail api inside of a Chrome Extension使用 Chrome 身份验证访问 Chrome 扩展程序中的 gmail api
【发布时间】:2015-09-21 17:24:57
【问题描述】:

为 Gmail 构建一个 Chrome 扩展程序,尝试使用 Chrome Auth 来通过 this StackOverflow postthe Gmail API docs 等获得对 gmail api 的访问权限。我使用chrome.identity.getAuthToken({ 'interactive': true }, function(token) {} 成功接收了令牌,但是当我将令牌插入请求 url 时,我收到以下 401 错误响应(代码如下)

错误响应

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "required",
        "message": "Login Required",
        "locationType": "header",
        "location": "Authorization"
      }
    ],
    "code": 401,
    "message": "Login Required"
  }
}

我的代码:
background.js

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete') {
    chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
      thisToken = token
      chrome.runtime.onMessage.addListener(
        function(request,sender,sendResponse){
          var gapiRequestUrlAndToken = "https://www.googleapis.com/gmail/v1/users/mail%40gmail.com/threads?key={" + thisToken + "}"

          var makeGetRequest = function (gapiRequestURL)
            {
                var xmlHttp = new XMLHttpRequest();
                xmlHttp.open( "GET", gapiRequestURL, false );
                xmlHttp.send( null );
                return xmlHttp.responseText;
            }

          makeGetRequest(gapiRequestUrlAndToken);
        }
      );
    });
  }
})

ma​​nifest.json

{
  "manifest_version": 2,
  "key": "<key>",
  "name": "exampleName",
  "description": "Description",
  "version": "0.0.1.7",
  "default locale": "en",
  "icons": { "128": "imgs/pledge_pin.png"},
  "content_scripts" : [
    {
      "matches": ["*://mail.google.com/mail/*"],
      "js": ["js/jquery.js", "js/compose.js", "bower_components/jqnotifybar/jquery.notifyBar.js"],
      "css": ["css/stylesheet.css", "bower_components/jqnotifybar/css/jquery.notifyBar.css"]
    }
  ],
  "background": {
    "scripts": ["scripts/background.js"]
  },
  "permissions": [
    "identity"
  ],
  "oauth2": {
    "client_id": "<client id>",
    "scopes": ["https://www.googleapis.com/auth/gmail.modify"]
  }
}

我怀疑这与我尝试将 Chrome Auth 用于 Gmail api 的事实有关,但我读过的其他帖子让我相信这是一个可行的选择。

如果我的代码没有泄露,我是新手,因此非常欢迎任何帮助,非常感谢您的宝贵时间。

【问题讨论】:

  • @Xan 通过编辑添加。

标签: google-chrome google-chrome-extension gmail gmail-api google-api-js-client


【解决方案1】:

key 用于通用应用程序机密。对于用户特定的令牌,您需要使用access_token。并且令牌不应包含在{} 中。如果 mail%40gmail.com 是您在 URL 中使用的实际值,它将不起作用。它需要是经过身份验证的用户的电子邮件地址或me

所以改变:

"https://www.googleapis.com/gmail/v1/users/mail%40gmail.com/threads?key={" + thisToken + "}"

到这里:

"https://www.googleapis.com/gmail/v1/users/me/threads?access_token=" + thisToken

【讨论】:

  • 太棒了!神圣的吸烟@abraham,非常感谢。当您的简单密钥与 access_token 节省了一天时,我正处于将所有内容拆开并尝试完全不同的方法的过程中。非常感谢你帮助我减少了新手程序员生活中的压力:-)
猜你喜欢
  • 2017-03-08
  • 2016-09-26
  • 2023-01-07
  • 2015-12-27
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 2011-11-20
相关资源
最近更新 更多