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

标签 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];
                  }
              }
          }];
 }
}

我收到客户端错误 (401)。我的要求有什么遗漏吗?

最佳答案

正确的范围是 "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 ")
                }
            }
        })
    }

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

关于objective-c - 如何使用 google contacts api 在 iOS 应用程序中获取 gmail 联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26878653/

相关文章:

ios - 核心数据迁移 : Possible to delete old store except for a few specific objects?

ios - 自动布局、约束和动画

api - 如何从Gmail导入联系人?

wordpress - 在 Google plus 中发布 Wordpress 帖子?

objective-c - Cocoa:从另一个类调用应用程序委托(delegate)方法

ios - objective-c - 如何判断 NSURL 是否被格式化为电话号码

c# - 如何列出特定文件夹中的文件夹和文件

javascript - Google API 不会翻译外文字母。

ios - 适用于iOS的Google+按钮

url - 如何以编程方式检查 Google 用户的个人资料图片是否不是默认图片?