Fetch only MyContacts from gmail account in iOS

972 Views Asked by At

I am trying to fetch gmail & yahoo contacts in my iPhone application.

For Gmail I have used GTMOauth2.0. I can see all contacts but when I want only contacts from MyContacts group. I have used following code to get contacts:

-(void)signInToGoogle:(id)sender
{
[self signOutFromGoogle];

NSString *keychainItemName = nil;
NSString *scope =  @"https://www.google.com/m8/feeds/contacts/default/full";

NSString *clientID = @"CLIENTID";
NSString *clientSecret = @"CLIENTSECRETID";

SEL finishedSel = @selector(viewController:finishedWithAuth:error:);

GTMOAuth2ViewControllerTouch *viewController;
viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
                                                          clientID:clientID
                                                      clientSecret:clientSecret
                                                  keychainItemName:keychainItemName
                                                          delegate:self
                                                  finishedSelector:finishedSel];

NSDictionary *params = [NSDictionary dictionaryWithObject:@"en" forKey:@"hl"];
viewController.signIn.additionalAuthorizationParameters = params;
NSString *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
viewController.initialHTMLString = html;
[[self navigationController] pushViewController:viewController animated:YES];
}

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
  finishedWithAuth:(GTMOAuth2Authentication *)auth
             error:(NSError *)error {
[[NSNotificationCenter defaultCenter] removeObserver:self];

if (error != nil) {

    [processObj removeFromSuperview];
    self.view.userInteractionEnabled = YES;

    NSLog(@"Authentication error: %@", error);
    NSData *responseData = [[error userInfo] objectForKey:@"data"]; // kGTMHTTPFetcherStatusDataKey
    if ([responseData length] > 0) {
        // show the body of the server's authentication failure response
        NSString *str = [[NSString alloc] initWithData:responseData
                                              encoding:NSUTF8StringEncoding];
        NSLog(@"%@", str);
    }

    self.auth = nil;
} else {
    self.auth = auth;
    [self doAnAuthenticatedAPIFetch];
 }
}

- (void)doAnAuthenticatedAPIFetch {
NSString *urlStr = @"https://www.google.com/m8/feeds/groups/default/full/Contacts";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[self.auth authorizeRequest:request
          completionHandler:^(NSError *error) {
              NSString *output = nil;
              if (error) {
                  output = [error description];
              } else {
                  NSURLResponse *response = nil;
                  NSData *data = [NSURLConnection sendSynchronousRequest:request
                                                       returningResponse:&response
                                                                   error:&error];
                  if (data) {
                      // API fetch succeeded :Here I am getti
                      output = [[NSString alloc] initWithData:data
                                                     encoding:NSUTF8StringEncoding];
                  } else {
                      // fetch failed
                      output = [error description];
                  }
              }
          }];
}

In the API I am passing "Contacts" as a group id but it is returning error "Group Id Not Found". I have the google document from https://developers.google.com/google-apps/contacts/v3/?csw=1

but still can't solve the problem. Help me on these.

1

There are 1 best solutions below

0
On

You'll need to fetch the groups feed to get the ID for a group. See the groups feed documentation, or try the ContactsSample app provided with the Google Data APIs Objective-C Client Library.