Search code examples
google-chromegoogle-chrome-extensiongmailgmail-apigoogle-api-js-client

gmail api returning emails outside of query parameters


Building a Chrome Extension for Gmail, trying to retrieve only emails addressed to me. I use the gapi thread API Explorer at the bottom of this page to test. It returns the inbox-only items as expected as you can see at the bottom of the image below. I copy and paste the request URL
https://www.googleapis.com/gmail/v1/users/me/threads?=to%3Adan%40pledgmail.com+in%3Ainbox&access_token= + thisToken
from the API Explorer above into my background.js code below, but I am returned emails that I have sent in addition to those I have received.

Note: I do change the "key" in the request URL from the API Explorer to "access_token", else no request I make works.

(In case my code doesn't give it away, I'm a newbie. Any help is sincerely appreciated, and I am grateful for your time.)


Google API Explorer results (expected)
this image

My code from background.js with copied request 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) {
              });
            });

          }
        );
      });
    }
  })

Return with unexpected 9 emails instead of 6 (top 3 are emails I sent)

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

My manifest.json for good measure

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

Solution

  • Doing threads.list() will return threads where any message in the thread matches the criteria. If you only want messages that match a specific criteria then do messages.list() instead.