2013-10-29 66 views
1

我正嘗試在我的iPhone應用程序中獲取gmail &雅虎聯繫人。從iOS中的gmail帳戶只提取我的聯繫人

對於Gmail,我使用了GTMOauth2.0。我可以看到所有聯繫人,但是隻需要MyContacts組中的聯繫人。我用下面的代碼獲取聯繫人:

-(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]; 
        } 
       } 
      }]; 
} 

在API我傳遞「聯繫人」爲一組ID,但它返回錯誤「羣組ID未找到」。我有谷歌文檔 https://developers.google.com/google-apps/contacts/v3/?csw=1

但仍不能解決問題。幫助我解決這些問題。

+0

我在做同樣的事情,試圖讓Gmail聯繫,但沒有得到任何解決方案。如果你得到答案,請讓我知道它是如何可能的。我和你的代碼一樣。 – virantporwal

+0

嘿,你讓它工作?請回復@virantporwal – va05

回答

相關問題