2017-04-22 204 views
0

我正在尝试使用Google联系人API获取Google联系人信息。如何从谷歌联系人API获取谷歌联系人?

我从谷歌所有的身份验证,但我不知道如何调用谷歌联系api。我这样做已经:

NSLog(@"\n\n User Access Token : %@",user.authentication.accessToken); 
NSLog(@"\n\n User Refresh Token : %@",user.authentication.refreshToken); 
NSLog(@"\n\n Person Token Expire Date : %@",user.authentication.accessTokenExpirationDate); 
NSLog(@"\n\n Person Client ID : %@",user.authentication.clientID); 

我想下面的代码调用API接触,但得到意外的响应:

响应:文件过早结束。

NSString * urlString = [NSString stringWithFormat:@"https://www.google.com/m8/feeds/contacts/default/full/"]; 
    urlString =[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] 
                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval:60.0]; 
    //do post request for parameter passing 
    [theRequest setHTTPMethod:@"POST"]; 

    [theRequest setValue:@"application/atom+xml" forHTTPHeaderField:@"Content-Type"]; 

    [theRequest addValue:[NSString stringWithFormat:@"Bearer %@",user.authentication.accessToken] forHTTPHeaderField:@"Authorization"]; 
    [theRequest addValue:@"3.0" forHTTPHeaderField:@"GData-Version"]; 

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

这里我取得联系,但在我的应用程序是如何得到的。

Google OAuth 2.0 Playground

在此先感谢。

回答

0

根据相关文档:

下面是对谷歌联系人API的OAuth 2.0范围信息:

- https://www.google.com/m8/feeds/ - 读取联系人和联系人群组

/写访问 - https://www.googleapis.com/auth/contacts.readonly - 对联系人和联系人组的只读访问

然后按照一些SO有关的问题,这些代码做参考 - How to fetch gmail Contacts in iOS application using google contacts api?Get Google contacts using API on iOS

斯威夫特

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 ") 
       } 
      } 
     }) 
    } 

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

希望这有助于。

+0

请给我完美的源代码。我找不到'self.auth'。 @Rebot先生 – Application

+0

我在哪里可以找到'authorizeRequest'? @ Mr.Rebot – Application