如何使用google contacts api在iOS应用程序中获取gmail联系人?

CKT*_*CKT 6 objective-c google-api google-plus google-oauth

在我的应用程序中,我们保留了通过gmail登录的选项.我要求检索Gmail联系人.

在下面的方法中,我使用AUTH对象(一旦成功)通过创建具有URL请求抓取Gmail联系人:" https://www.google.com/m8/feeds/contacts/default/full "

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
               error:(NSError *)error {
if(!error) {

auth.clientID  =myClientId;
auth.clientSecret  =myClientSecret;
auth.scope= @"https://www.googleapis.com/auth/contacts.readonly";

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

NSURL *url = [NSURL URLWithString:urlStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"3.0" forHTTPHeaderField:@"GData-Version"];
[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];
                      NSLog(@"%@",output);
                  } else {
                      // fetch failed
                      output = [error description];
                  }
              }
          }];
 }
}
Run Code Online (Sandbox Code Playgroud)

我收到客户端错误(401).我有什么要求我的要求.

jos*_*405 0

正确的范围是"https://www.google.com/m8/feeds"

迅速

class func getContactsFromUser() {
        let urlStr = "https://www.google.com/m8/feeds/contacts/default/full"
        let url = NSURL(string: urlStr);

        var request = NSMutableURLRequest(URL: url!)

        let appd = UIApplication.sharedApplication().delegate as! AppDelegate
        let error: NSError!
        appd.service.authorizer.authorizeRequest!(request, completionHandler: { (error) -> Void in

            if error != nil {
                println("error getting contacts is \(error.localizedDescription)")
            } else {
                let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil

                let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: nil)

                if data != nil {
                    let stringResponse = NSString(data: data!, encoding: NSUTF8StringEncoding)
                    println("**** stringResponse **** \(stringResponse!)")
                } else {
                    println("error 2 getting contacts is ")
                }
            }
        })
    }
Run Code Online (Sandbox Code Playgroud)

在目标 c

- (void)doAnAuthenticatedAPIFetch {
NSString *urlStr = @"https://www.google.com/m8/feeds/contacts/default/full";
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];
                  }
              }
          }];
}
Run Code Online (Sandbox Code Playgroud)