google-chrome - 使用 Chrome 身份验证访问 Chrome 扩展中的 gmail api

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

为 Gmail 构建一个 Chrome 扩展,尝试使用 Chrome Auth 获得对 gmail api 的访问权 this StackOverflow postthe Gmail API docs ,等等。我使用 chrome.identity.getAuthToken({ 'interactive': true }, function(token) {} 成功接收到 token ,但是当我将 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);
        }
      );
    });
  }
})

list .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 for Gmail api 这一事实有关,但我读过的其他帖子让我相信这是一个可行的选择。

以防我的代码没有泄露,我是新手,所以非常欢迎任何帮助,非常感谢您抽出时间。

最佳答案

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

所以改变:

"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

关于google-chrome - 使用 Chrome 身份验证访问 Chrome 扩展中的 gmail api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31214096/

相关文章:

google-chrome - 有没有办法阻止谷歌浏览器在空白标签上显示最常访问的网站?

javascript - 如何在chrome开发者工具中添加伪元素?

android - 如何创建选择器 Intent 以从 gmail 应用程序、yahoo 邮件应用程序或浏览器中进行选择?

oauth - 为 gmail imap 生成有效的 oauth token 和密码?

javascript - Chrome : Tainted canvases may not be exported; Offline-only app

java - Android- EXTRA_HEADERS 未反射(reflect)在更新的 Chrome 版本大于 83 中

google-chrome-extension - 我可以在用户授予权限后重新打开我的弹出窗口吗?

google-chrome-extension - chrome打开和关闭时如何以 "background page"开始事件

javascript - Chrome 扩展选项卡 onUpdated 事件

Android:如何使用 intent-filter 处理 PDF 电子邮件附件?