gmail api在查询参数之外返回电子邮件

时间:2015-07-07 19:19:41

标签: google-chrome google-chrome-extension gmail gmail-api gapi

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

注意:我确实更改了"键"在从API资源管理器到#34; access_token"的请求URL中,否则我没有任何请求可用。

(如果我的代码没有放弃,我是新手。真心感谢任何帮助,我很感激你的时间。)


Google API Explorer结果(预期)
this image

我的带有复制请求网址的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 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"]
  }
}

2 个答案:

答案 0 :(得分:7)

执行threads.list()将返回线程中的任何消息与条件匹配的线程。如果您只想要符合特定条件的消息,请改为messages.list()

答案 1 :(得分:1)

您的代码中存在拼写错误,使您的代码与API Explorer的行为不同。你错过了' q'在查询字符串中,结果包括所有没有过滤器的电子邮件。

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

尝试一下摆脱滋扰。