本文介绍了如何使用谷歌联系人API在iOS应用程序中获取Gmail联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我们保留了通过Gmail登录的选项。我有要求检索Gmail联系人。
在以下方法中,我使用auth对象(一旦成功)通过使用url创建请求来获取gmail联系人:
$ b $ ($ 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.0forHTTPHeaderField:@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获取成功:这里我是getti
output = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@%@,output);
} else {
//获取失败
输出= [错误描述];
}
}
}];
$ b我得到客户端错误(401)。是否有任何东西我缺少我的请求。解决方案正确的范围是https: //www.google.com/m8/feeds
在swift中
class func getContactsFromUser(){
let urlStr =https://www.google.com/m8/feeds/contacts/default/full
let url = NSURL(string:urlStr);
var request = NSMutableURLRequest(URL:url!)
让appd = UIApplication.sharedApplication()。delegate as! AppDelegate
让错误:NSError!
appd.service.authorizer.authorizeRequest!(request,completionHandler:{(error) - >无效
if error!= nil {
println(error getting contacts是\(error.localizedDescription))
} else {
let response:AutoreleasingUnsafeMutablePointer< NSURLResponse?> = nil
let data = NSURLConnection.sendSynchronousRequest(request,returningResponseResponse :response,error:nil)
if data!= nil {
let stringResponse = NSString(data:data !, encoding:NSUTF8StringEncoding)
println(**** stringResponse **** \(stringResponse!))
} else {
println(error 2 getting contacts is)
}
}
})
$ b 在目标c中
- (void)doAnAuthenticatedAPIFetch {
N SString * 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获取成功:这里我是getti
output = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
} else {
//获取失败
输出= [错误描述];
}
}
}];
}
In my application we kept option to login through gmail. I have requirement to retrieve gmail contacts.
In the following method i am using auth object(once success) to fetch gmail contacts by creating request with url: "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];
}
}
}];
}
}
I'm getting client error(401). is there any thing i'm missing to my request.
解决方案 The correct Scope is "https://www.google.com/m8/feeds"
In swift
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 ")
}
}
})
}
In 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];
}
}
}];
}
这篇关于如何使用谷歌联系人API在iOS应用程序中获取Gmail联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!