Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in Gmail API to include extra fields (e.g. subject, body) in the message list method?

Tags:

gmail-api

The "Try it" part documentation allows me to play the API, and the field selector allow me to select a lot of fields, e.g. header, raw, etc. But none of them actually showed up when tried the API. The only thing I saw were still just the message ID and the thread ID.

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

E.g. The following:

GET https://www.googleapis.com/gmail/v1/users/{user_id}/messages?**fields=messages(historyId%2Cid%2Cpayload%2Craw%2CsizeEstimate%2Csnippet%2CthreadId)**&key={YOUR_API_KEY}

Returns:

{
 "messages": [
  {
   "id": "146da54fe3dc089e",
   "threadId": "146da54fe3dc089e"
  },
  {
   "id": "146da41d9486982f",
   "threadId": "146da41d9486982f"
  },
  ...
}

But I would expect the extra fields requested are returned too.

Is there a way to get this working? I know there is a separate method to get an individual message but like to get them batch if possible.

like image 945
yuklai Avatar asked Jun 26 '14 22:06

yuklai


1 Answers

messages.list does not return much more than just the identifiers. not sure what the field selector is but i don't believe it's used.

however you can use a batched message.get to then retrieve many messages at once in a second call:

A batch request consists of multiple API calls combined into one HTTP request. This section describes the batch syntax in detail; later, there's an example.

Note: A set of n requests batched together counts toward your usage limit as n requests, not as one request. The batch request is taken apart into a set of requests before processing.

From: https://developers.google.com/storage/docs/json_api/v1/how-tos/batch

With the Gmail API and batch here're some sample code:

GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery];

[gmailMessageIds enumerateObjectsUsingBlock:^(NSNumber *messageId, NSUInteger idx, BOOL *stop) {
    GTLQueryGmail *query = [GTLQueryGmail queryForUsersMessagesGet];
    query.userId = self.account.email;
    query.identifier = [NSString stringWithFormat:@"%llx", [messageId unsignedLongLongValue]];
    query.format = kGTLGmailFormatRaw;

    [batchQuery addQuery:query];
}];


[self.gmailService executeQuery:batchQuery completionHandler:^(GTLServiceTicket *ticket, GTLBatchResult *result, NSError *error) {
    NSArray *gmailMessages = result.successes.allValues; // This is an array of GTLGmailMessage objects
    ... 
}];
like image 123
Eric D Avatar answered Sep 27 '22 23:09

Eric D