8

I'm working on email platform (in Objective-C language) and want to fetch some mails using GTMHTTPFetcher and GTMOAuth2Authentication frameworks. I'm using gmail APIs for getting userinfo and getting appropriate response.

I want to fetch emails for the user's inbox with category; I'm thinking to use the SYSTEM level labels such as CATEGORY_SOCIAL for social, CATEGORY_PERSONAL For personal/primary, etc.

For this functionality, I'm using following GET API: https://www.googleapis.com/gmail/v1/users/userId/messages API with proper parameters. I'm using google's try it out option for this. https://developers.google.com/gmail/api/v1/reference/users/messages/list#try-it

Problem: I'm able to get all the messageIDs/threadIDs, but not able to get labelIDs in the google developer console. I've also tried this GET method from the Objective-C code, but didn't get the labelIDs.

I've attached the code snippet for the Objective-C code, Can you please help me out for this problem?

NSString *newAPIStr = @"";

newAPIStr = [NSString stringWithFormat:@"https://www.googleapis.com/gmail/v1/users/%@/messages?fields=messages(id,labelIds,threadId),nextPageToken&maxResults=%d",emailStr,maxResult];


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 = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kMyClientID clientSecret:kMyClientSecret];
GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[myFetcher setAuthorizer:currentAuth];
[myFetcher beginFetchWithCompletionHandler:^(NSData *retrievedData, NSError *error) {
    if (error != nil) {
        // status code or network error
    } else {
        // succeeded
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:retrievedData options:kNilOptions error:&error];
        NSArray* messageArray =[json objectForKey:@"messages"];
        NSString *nextPageToken = [json objectForKey:@"nextPageToken"];
        for (NSDictionary *dictionary in messageArray) {
            [[EmailService instance].primaryMessages addObject:[dictionary objectForKey:@"id"]];
        }

        NSMutableArray *pArray = [[EmailService instance] primaryMessages];
        [[NSUserDefaults standardUserDefaults] setObject:pArray forKey: ALL_FUNNL];
        [[NSUserDefaults standardUserDefaults] setObject:nextPageToken forKey:@"PRIMARY_PAGE_TOKEN"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        if([EmailService instance].primaryMessages.count < 5000)
            [self getPrimaryMessages:emailStr nextPageToken:nextPageToken numberOfMaxResult:100];
        else
            NSLog(@"----- Primary messages count > %d",pArray.count);
    }
}];}

Getting output as follows:

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

Krunal
  • 240
  • 1
  • 3
  • 6

3 Answers3

4

message.list() only returns the ids of the messages, as is pretty standard REST behavior to keep the response small and fast. If you also need to get more info on the messages (such as the labels) you'd need to followup with something like message.get(id=$THAT_ID, format=MINIMAL), which you can call using batch to retrieve for many messages in parallel.

Eric D
  • 6,901
  • 1
  • 15
  • 26
  • Could you please elaborated more? – Krunal Aug 26 '14 at 11:01
  • 11
    I still think that it's very annoying that Googles APIs Explorer has the ability to enter `fields` values for _Users.messages:list_ while these are not returned in the response or not even an error that cetain fields values are not valid. – Kapé Mar 13 '15 at 22:28
  • @Kapé worst than that, on the documentation page itself it lets you choose other fields that are not id or labelId, like you can see on the link below but it will give you an empty response if you go that way. It won't throw an error or give you any feedback what so ever maybe directing you to get() or something. https://developers.google.com/apis-explorer/#p/gmail/v1/gmail.users.messages.list – Murilo Nov 01 '17 at 20:18
1

I believe you should be doing it the other way around. Get the list of labels and for each label get the messages.

INBOX itself is a label which will let you get the Primary emails (emails in the 'Primary' tab)

Get the list of labels here: https://developers.google.com/gmail/api/v1/reference/users/labels/list

When getting messages provide the labelId(s).

Also CATEGORY_UPDATES, INBOX, CATEGORY_PROMOTIONS are itself Ids and as well as names.

I hope this helps to handle your requirement.

Satish P
  • 514
  • 2
  • 6
0

After getting the response as

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

You can use the following method to get the Label IDs from each mail using the id,

https://www.googleapis.com/gmail/v1/users/<userId>/messages/<messageId>

Reference : Users.messages: get

AbdulRahman Ansari
  • 3,007
  • 1
  • 21
  • 29
  • The documentation of Gmail API doesn't say that although this works I think messages.list has a bug for not bringing other fields. On the documentation itself it has a "field" attribute which gives you an option to choose others fields other than id and labelid. https://developers.google.com/apis-explorer/#p/gmail/v1/gmail.users.messages.list – Murilo Nov 01 '17 at 20:16