I have followed the steps at: https://developers.google.com/gmail/api/quickstart/ios
and I can successfully get Email List, but I need to read each one.
This is how I do:
- (void)fetchEmails {
self.output.text = @"Getting emails...";
NSString *newAPIStr = @"";
newAPIStr = [NSString stringWithFormat:@"https://www.googleapis.com/gmail/v1/users/me/profile"];
NSURL *url = [NSURL URLWithString:newAPIStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"GET"];
GTMOAuth2Authentication *currentAuth= self.service.authorizer;
GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[myFetcher setAuthorizer:currentAuth];
[myFetcher beginFetchWithCompletionHandler:^(NSData *retrievedData, NSError *error) {
if (error != nil)
{
// status code or network error
NSLog(@"Ha ocurrido un error");
}
else {
// succeeded
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:retrievedData options:kNilOptions error:&error];
NSLog(@"json==>%@",json);
userid=[json objectForKey:@"emailAddress"];
GTLQueryGmail *query = [GTLQueryGmail queryForUsersMessagesList];
[self.service executeQuery:query delegate:self didFinishSelector:@selector(displayResultMessageListWithTicket:finishedWithObject:error:)];
}
}];
}
I have to call "profile" because I don't know another way to get userid, and then I get the list of emails.
- (void)displayResultMessageListWithTicket:(GTLServiceTicket *)ticket
finishedWithObject:(GTLGmailListMessagesResponse *)emailsResponse error:(NSError *)error
{
if (error == nil)
{
NSMutableString *labelString = [[NSMutableString alloc] init];
NSLog(@"emailsResponse =%@",emailsResponse);
if (emailsResponse.messages.count > 0)
{
[labelString appendString:@"Labels:\n"];
NSMutableArray *gmailMessageIds = [[NSMutableArray alloc] init];
for (GTLGmailMessage *message in emailsResponse.messages)
{
NSString *mess = message.identifier;
[gmailMessageIds addObject:mess];
}
GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery];
[gmailMessageIds enumerateObjectsUsingBlock:^(NSString *messageId, NSUInteger idx, BOOL *stop) {
GTLQueryGmail *query = [GTLQueryGmail queryForUsersMessagesGet];
//query.userId = ;
query.identifier = [NSString stringWithFormat:@"%llx", [messageId unsignedLongLongValue]];
query.format = kGTLGmailFormatRaw;
[batchQuery addQuery:query];
}];
[self.service executeQuery:batchQuery completionHandler:^(GTLServiceTicket *ticket, GTLBatchResult *result, NSError *error) {
NSArray *gmailMessages = result.successes.allValues; // This is an array of GTLGmailMessage objects
for (GTLGmailMessage *message in gmailMessages)
{
[labelString appendFormat:@"%@\n", message.JSONString];
}
}];
} else {
[labelString appendString:@"No emails found."];
}
self.output.text = labelString;
} else {
[self showAlert:@"Error" message:error.localizedDescription];
}
}
The problem that I have is that gmailMessages contains 100 items without content
GTLBatchResult 0x7ffc39d66a50 (successes:100 failures:0)
And iterating it... each message has 0 key/value pairs
message GTLGmailMessage * 0 key/value pairs 0x00007ffc39d50700
Exists another way to get each email? I tryied also with:
GTLQueryGmail *query = [GTLQueryGmail queryForUsersMessages];
query.identifier = [NSString stringWithFormat:@"%llx", [messageId unsignedLongLongValue]];
query.format = kGTLGmailFormatRaw;
[self.service executeQuery:query delegate:self didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
But I think I have the same problem.
I also have to say that I copied GTLBatchQuery from this post and I have to change NSNumber *messageId
to NSString *messageId
:
[gmailMessageIds enumerateObjectsUsingBlock:^(NSNumber *messageId, NSUInteger idx, BOOL *stop)