我尝试使用Google通讯录APi获取所有谷歌联系人,但我没有找到适用于iOS的任何内容。 OAuth无效,因为Google更改了使用WebView OAuth的政策。我使用Google登录框架获取访问令牌,但此令牌对谷歌联系人API无效: https://www.google.com/m8/feeds/contacts/default/full
请建议使用最新的iOS SDK 10+和最新的google框架来获取google / gmail联系人
答案 0 :(得分:0)
尝试使用适用于iOS的GIDSignIn Class来解决您的OAuth问题。
请参阅"Google Sign-In for iOS"以下是使用GIDSignIn的示例代码:
您还可以查看此SO thread以获取更多信息。
答案 1 :(得分:0)
I)尝试GIDSignIn Class进行身份验证:
II)导入所需的类作为休假:
#import <UIKit/UIKit.h>
#import <GoogleSignIn/GoogleSignIn.h>
@interface ViewController : UIViewController<GIDSignInDelegate,GIDSignInUIDelegate,NSURLSessionDelegate>
@end
III)在viewDidLoad中充当休假:
- (void)viewDidLoad {
[super viewDidLoad];
[GIDSignIn sharedInstance].delegate = self;
[GIDSignIn sharedInstance].uiDelegate = self;
[GIDSignIn sharedInstance].clientID = "your_CLIENT_ID; //get from console
[[GIDSignIn sharedInstance] setScopes:@[@"https://www.googleapis.com/auth/plus.login",@"https://www.googleapis.com/auth/plus.me",@"https://www.googleapis.com/auth/contacts",@"https://www.googleapis.com/auth/contacts.readonly"]];
// Button for opening Google Auth //
GIDSignInButton *button = [[GIDSignInButton alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 200.0f)];
button.style = kGIDSignInButtonStyleWide;
button.colorScheme =kGIDSignInButtonColorSchemeLight;
[button setCenter:self.view.center];
[self.view addSubview:button];
}
IV)用户完成登录后,请按GIDSignInUIDelegate
进行如下操作:
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
// Perform any operations on signed in user here.
NSString *userId = user.userID; // For client-side use only!
NSString *idToken = user.authentication.idToken; // Safe to send to the server
NSString *fullName = user.profile.name;
NSString *givenName = user.profile.givenName;
NSString *familyName = user.profile.familyName;
NSString *email = user.profile.email;
[self getPeopleList];
// ...
}
// Implement these methods only if the GIDSignInUIDelegate is not a subclass of
// UIViewController.
// Stop the UIActivityIndicatorView animation that was started when the user
// pressed the Sign In button
- (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
NSLog(@"0.0.");
}
// Present a view that prompts the user to sign in with Google
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController {
[self presentViewController:viewController animated:YES completion:nil];
}
// Dismiss the "Sign in with Google" view
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
V)现在在-(void)getPeopleList{}
中,您将找到联系人:
-(void)getPeopleList{
NSString *url = [NSString stringWithFormat:@"https://people.googleapis.com/v1/people/me/connections?personFields=emailAddresses"];
NSURL *linkurl = [NSURL URLWithString:url];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:linkurl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request addValue:[NSString stringWithFormat:@"Bearer %@",[GIDSignIn sharedInstance].currentUser.authentication.accessToken] forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"GET"];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSDictionary *user_contact_json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"user_contacts %@", user_contact_json);
} else {
NSLog(@"Error %@",error);
}
}];
[postDataTask resume];
}