title: Gmail API Users.messages: list
body: <p>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. </p>
  
  <p>I want to fetch emails for the user's inbox with category; I'm thinking to use the SYSTEM level labels such as <strong>CATEGORY_SOCIAL</strong> for social, <strong>CATEGORY_PERSONAL</strong> For personal/primary, etc.</p>
  
  <p>For this functionality, I'm using following GET API: <a href="https://www.googleapis.com/gmail/v1/users/userId/messages" rel="noreferrer">https://www.googleapis.com/gmail/v1/users/userId/messages</a> API with proper parameters. I'm using google's try it out option for this.
  <a href="https://developers.google.com/gmail/api/v1/reference/users/messages/list#try-it" rel="noreferrer">https://developers.google.com/gmail/api/v1/reference/users/messages/list#try-it</a> </p>
  
  <p>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.</p>
  
  <p>I've attached the code snippet for the Objective-C code, Can you please help me out for this problem? </p>
  
  <pre><code>NSString *newAPIStr = @"";
  
  newAPIStr = [NSString stringWithFormat:@"https://www.googleapis.com/gmail/v1/users/%@/messages?fields=messages(id,labelIds,threadId),nextPageToken&amp;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:&amp;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 &lt; 5000)
              [self getPrimaryMessages:emailStr nextPageToken:nextPageToken numberOfMaxResult:100];
          else
              NSLog(@"----- Primary messages count &gt; %d",pArray.count);
      }
  }];}
  </code></pre>
  
  <p>Getting output as follows:</p>
  
  <p><code>{
   "messages": [
    {
     "id": "146da54fe3dc089e",
     "threadId": "146da54fe3dc089e"
    },
    {
     "id": "146da41d9486982f",
     "threadId": "146da41d9486982f"
    },
    ...
  }</code></p>