【问题标题】:Fetch only MyContacts from gmail account in iOS仅从 iOS 中的 gmail 帐户获取 MyContacts
【发布时间】:2013-11-09 01:14:27
【问题描述】:

我正在尝试在我的 iPhone 应用程序中获取 gmail 和 yahoo 联系人。

对于 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

但仍然无法解决问题。帮我解决这些问题。

【问题讨论】:

  • 我也在做同样的事情并尝试获取 Gmail 联系人,但没有得到任何解决方案。如果您得到答案,请告诉我这怎么可能。我和你的代码一样。
  • 嘿,你成功了吗?请回复@virantporwal

标签: objective-c gmail google-api-objc-client


【解决方案1】:

您需要获取群组供稿以获取群组的 ID。查看groups feed documentation,或尝试Google Data APIs Objective-C Client Library 提供的ContactsSample 应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2014-09-08
    • 2018-05-18
    • 1970-01-01
    • 2014-12-31
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多