GMail v1 API Users.messages results differ from We

2019-02-26 03:24发布

I'm using the following query:

No Man's Sky

I get different results from the Web UI compared with the API Explorer and my C# application.

GMail Web UI

Web UI

Note, conversation view is disabled:

Conversation Setting

If I enable conversation view, I get 9 threads returned by the Web UI; using the thread API, I get 7 threads.


API Explorer at https://developers.google.com/gmail/api/v1/reference/users/messages/list:

API Explorer

{  
 "messages": [  
  {  
   "id": "15686dcf7989e60f"  
  },  
  {  
   "id": "1566bce4412a439d"  
  },  
  {  
   "id": "156327e51b979f48"  
  },  
  {  
   "id": "1560f0caf9297ab9"  
  },  
  {  
   "id": "155f01dba1c3ad77"  
  },  
  {  
   "id": "1538bce006a95a84"  
  },  
  {  
   "id": "15342e9d99b4d3f6"  
  }  
 ]  
}  

I see the same results as the API explorer in my C# application. There seems to be a disconnect here between the API and Web UI. The Web UI returns 10 results, which the API returns only 7 of those 10 (I have verified that they're a subset of the 10 manually).

UPDATE:

It turns out that the API treats the query as a literal. Surrounding the query in the Web UI with quotation marks gives the same results as the API.

This does leave the question of how to replicate the original search using the API; does one have to run 3 queries and then take only the results returned in all 3? When you start getting into complex queries that becomes a bit of a parsing chore.

With literals

UPDATE 2

I've tried to replicate the Web UI results by running the 3 ANDed terms individually and then pulling results where the message ID is the result set of all 3 queries and I get the same 7 results as the API call.

Code used to test:

var service = GMailServiceManager.Instance.Service;
var resultsNo = GMailUtils.GetItems(service, null, "No", true);
var resultsMans = GMailUtils.GetItems(service, null, "Man's", true);

var resultsSky = GMailUtils.GetItems(service, null, "Sky", true);

var anded = from nos in resultsNo.Items
              from manses in resultsMans.Items
                from skys in resultsSky.Items
                where nos.Id == manses.Id && nos.Id == skys.Id
                select nos.Id;

GMailUtils.GetItems() simply handles batching of the API requests and some other app-specific routines. It uses the following to retrieve each group of items in the batch:

UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List("me");
request.Q = search;
request.Fields = "messages(id),nextPageToken";
request.PageToken = pageToken;
// Logging and batching here
...
  request.Execute();
...

I now have no idea how the Web UI comes up with the other hits; the hit highlighting is on www.no-mans-sky.com in two instances (they're in the same thread):

enter image description here

The other hits on No Man and Sky individually in an e-mail (note not Man's).

Update 3

Going back to the Web UI I decided to do a little testing on the search term Man's.

In the web UI, I get several hundred hits:

Web UI Man's

For "Man's" I get 11 hits:

Web UI Quoted Man's

The API matches the quoted Web UI:

{
 "messages": [
  {
   "id": "15686dcf7989e60f"
  },
  {
   "id": "1566bce4412a439d"
  },
  {
   "id": "156327e51b979f48"
  },
  {
   "id": "1560f0caf9297ab9"
  },
  {
   "id": "155f01dba1c3ad77"
  },
  {
   "id": "1538bce006a95a84"
  },
  {
   "id": "15342e9d99b4d3f6"
  },
  {
   "id": "13da4a6d7a4159b2"
  },
  {
   "id": "13da4a502eca7dff"
  },
  {
   "id": "12e70fbf3d655ac8"
  },
  {
   "id": "1278adc3ed72f6a8"
  }
 ]
}

Based on this it would appear that the API's query is treated as a string literal rather than Web-UI-style query. Seems like a bug to me, as I cannot find a way to reproduce the Web UI's results using the API.

标签: gmail-api
1条回答
【Aperson】
2楼-- · 2019-02-26 04:18

I tried it with my own Gmail, and I observed that some emails are found in a single thread.

Here is my example:

I used the word time tracker as my subject, and the Gmail UI gives me a result of 4 emails. But if you count the total results, it gives me a total of 12 results. 6 from A, 3 from B, 2 from C and 1 email from D.

enter image description here

So if I use the request

GET https://www.googleapis.com/gmail/v1/users/example%40google.com/messages?q=subject%3A(time+tracker)&fields=messages(id%2CthreadId)&key={YOUR_API_KEY}

or the Try it part in the Gmail API,

enter image description here

It will give me a result of 12 IDs like in the GMail UI.

enter image description here

Looking at the illustration above, you will notice that all emails belonging to the same thread have identical threadId.

So try to use the threadId to determine the number of emails found in your request. Please someone tell me if I misunderstood this question.

查看更多
登录 后发表回答