问题
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.
回答1:
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
...
}];
回答2:
Upon browsing the Google documentation, using the messages.get section, I was able to return all of the field values via json using the method getMessage(...)
.
In the original request you are given the id
and threadId
. Using the id we are able to return all messages by doing something like so:
ListMessagesResponse mResponse =
service.users().messages().list(user).execute();
// This will return the json listed with the field methods id and threadId.
List<Message> messages = mResponse.getMessages();
// Parse the response
if(message.size() == 0){...}
else {
for(Message msg: messages)
*.getMessage(service,user,msg.getId());
}
The above is not exact code but, should provide you with an idea of how to approach this problem.
来源:https://stackoverflow.com/questions/24441433/is-there-a-way-in-gmail-api-to-include-extra-fields-e-g-subject-body-in-the