【问题标题】:gmail api returning emails outside of query parametersgmail api返回查询参数之外的电子邮件
【发布时间】:2015-09-25 11:12:00
【问题描述】:

为 Gmail 构建 Chrome 扩展程序,尝试仅检索发给我的电子邮件。我使用this page底部的gapi线程API Explorer进行测试。如下图底部所示,它会按预期返回仅限收件箱的项目。我复制并粘贴请求 URL
https://www.googleapis.com/gmail/v1/users/me/threads?=to%3Adan%40pledgmail.com+in%3Ainbox&access_token= + thisToken
从上面的 API Explorer 到下面的 background.js 代码中,但是除了收到的邮件之外,我还收到了我发送的电子邮件。

注意:我确实将 API Explorer 中请求 URL 中的“密钥”更改为“access_token”,否则我发出的任何请求都不起作用。

(如果我的代码没有泄露,我是新手。真诚感谢任何帮助,感谢您的宝贵时间。)


Google API Explorer 结果(预期)

我的 background.js 代码带有复制的请求 URL

  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 gapiRequestAllThreadsToSelf = "https://www.googleapis.com/gmail/v1/users/me/threads?=to%3Adan%40pledgmail.com+in%3Ainbox&access_token=" + thisToken
            var getAllThreadsToSelf = function (gapiRequestURL)
              {
                  var xmlHttp = new XMLHttpRequest();
                  xmlHttp.open( "GET", gapiRequestURL, false );
                  xmlHttp.send( null );
                  return xmlHttp.responseText;
              }

            var threadsToSelf = getAllThreadsToSelf(gapiRequestAllThreadsToSelf)

            chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
              chrome.tabs.sendMessage(tabs[0].id, {data: threadsToSelf}, function(response) {
              });
            });

          }
        );
      });
    }
  })

返回意外的 9 封电子邮件而不是 6 封(前 3 封是我发送的电子邮件)

{
 "threads": [
  {
   "id": "14e69c9075bd53",
   "snippet": "Thank you!",
   "historyId": "8573"
  },
  {
   "id": "14e69be815c6a0",
   "snippet": "Thaaaanks",
   "historyId": "8550"
  },
  {
   "id": "14e644211d19b0",
   "snippet": "Reply to this email, Danny boy",
   "historyId": "8481"
  },
  {
   "id": "14e1c4702de573",
   "snippet": "Hey guys, Here is the gmail Chrome extension I am working on. This is the basic mvp I'm iterating",
   "historyId": "8328"
  },
  {
   "id": "14e13259f00f0e",
   "snippet": "Hello Daniel Klos, Thanks for buying from Chrome Web Store using Google Wallet! Chrome Web Store will",
   "historyId": "8431"
  },
  {
   "id": "14e12da5ca9c16",
   "snippet": "Here are your account details. Sign in » Your billing setup is complete. See your account details",
   "historyId": "6181"
  },
  {
   "id": "14e12d1e3e41ba",
   "snippet": "Hi Dan Welcome to your Gmail inbox Save everything With up to 30GB of space, you'll never need to",
   "historyId": "2678"
  },
  {
   "id": "14e12d1e1be7b3",
   "snippet": "Hi Dan Get the official Gmail app The best features of Gmail are only available on your phone and",
   "historyId": "6114"
  },
  {
   "id": "14e12d1e19e865",
   "snippet": "Hi Dan Work smarter with Gmail and Google Apps Manage Calendar meetings Google Calendar makes",
   "historyId": "2682"
  }
 ],
 "resultSizeEstimate": 9
}

我的 manifest.json 很好

{
  "manifest_version": 2,
  "key": "redacted",
  "name": "redacted",
  "description": "Description",
  "version": "0.0.2.0",
  "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": "redacted",
    "scopes": ["https://www.googleapis.com/auth/gmail.modify"]
  }
}

【问题讨论】:

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


    【解决方案1】:

    执行threads.list() 将返回线程中任何消息都符合条件的线程。如果您只希望 messages 符合特定条件,请改用messages.list()

    【讨论】:

    • Eric 和 @Xan 感谢您的帮助。我想我仍然不明白为什么当 API Explorer 和我的代码都发出相同的请求时,它们的行为会有所不同......?
    【解决方案2】:

    您的代码中有一个拼写错误,导致您的代码与 API Explorer 的行为方式不同。您在查询字符串中缺少“q”,这使得结果包括所有没有过滤器的电子邮件。

    var gapiRequestAllThreadsToSelf = "https://www.googleapis.com/gmail/v1/users/me/threads?q=to%3Adan%40pledgmail.com+in%3Ainbox&access_token=" + thisToken
    

    试试这个以摆脱麻烦。

    【讨论】:

      猜你喜欢
      • 2016-04-27
      • 2016-02-28
      • 2019-11-07
      • 2014-01-08
      • 2021-09-11
      • 2015-03-23
      • 2016-11-25
      • 2018-07-21
      • 1970-01-01
      相关资源
      最近更新 更多