使用Google+ iOS API如何获取login的用户个人资料详细信息?

我已经成功地从iPhone应用程序login到谷歌加。 但如何获得login用户的详细信息? 如个人资料ID,电子邮件等

我试过这个, Stackoverflow的答案类似的问题,但我无法得到它的工作。 在这个例子中,究竟传递给accessTocken的是什么,

NSString *str = [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",accessTocken]; 

我在- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error { }方法中实现了该代码。 但是auth.accessToken返回一个空值。

所以我不能使用auth.accessToken来追加这个URL。 有没有其他方法来完成这个工作?

 - (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error { NSLog(@"Received Error %@ and auth object==%@", error, auth); if (error) { // Do some error handling here. } else { [self refreshInterfaceBasedOnSignIn]; GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"]; NSLog(@"email %@ ", [NSString stringWithFormat:@"Email: %@",[GPPSignIn sharedInstance].authentication.userEmail]); NSLog(@"Received error %@ and auth object %@",error, auth); // 1. Create a |GTLServicePlus| instance to send a request to Google+. GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ; plusService.retryEnabled = YES; // 2. Set a valid |GTMOAuth2Authentication| object as the authorizer. [plusService setAuthorizer:[GPPSignIn sharedInstance].authentication]; // 3. Use the "v1" version of the Google+ API.* plusService.apiVersion = @"v1"; [plusService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLPlusPerson *person, NSError *error) { if (error) { //Handle Error } else { NSLog(@"Email= %@", [GPPSignIn sharedInstance].authentication.userEmail); NSLog(@"GoogleID=%@", person.identifier); NSLog(@"User Name=%@", [person.name.givenName stringByAppendingFormat:@" %@", person.name.familyName]); NSLog(@"Gender=%@", person.gender); } }]; } } 

希望对你有帮助

这是获取当前login用户的电子邮件ID的最简单和最简单的方法首先创build一个GPPSignIn类的实例variables

 GPPSignIn *signIn; 

然后在viewDidLoad中初始化它

 - (void)viewDidLoad { [super viewDidLoad]; static NSString * const kClientID = @"your client id"; signIn = [GPPSignIn sharedInstance]; signIn.clientID= kClientID; signIn.scopes= [NSArray arrayWithObjects:kGTLAuthScopePlusLogin, nil]; signIn.shouldFetchGoogleUserID=YES; signIn.shouldFetchGoogleUserEmail=YES; signIn.delegate=self; } 

接下来在你的视图控制器中实现GPPSignInDelegate这里你可以获得login用户的电子邮件ID

  - (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error { NSLog(@"Received Access Token:%@",auth); NSLog(@"user google user id %@",signIn.userEmail); //logged in user's email id } 

首先,请按照此说明在Google API Console中设置您的项目(并下载configuration文件GoogleService-Info.plist),并将最新的GoogleSignIn SDK
纳入您的项目: 集成指南

一段代码。

这里是获取最新版本的GoogleSignIn SDK的用户信息的代码。

 #import <GoogleSignIn/GoogleSignIn.h> //... @interface ViewController() <GIDSignInDelegate, GIDSignInUIDelegate> //... - (IBAction)googleSignInTapped:(id)sender { [GIDSignIn sharedInstance].clientID = kClientId;// Replace with the value of CLIENT_ID key in GoogleService-Info.plist [GIDSignIn sharedInstance].delegate = self; [GIDSignIn sharedInstance].shouldFetchBasicProfile = YES; //Setting the flag will add "email" and "profile" to scopes. NSArray *additionalScopes = @[@"https://www.googleapis.com/auth/contacts.readonly", @"https://www.googleapis.com/auth/plus.login", @"https://www.googleapis.com/auth/plus.me"]; [GIDSignIn sharedInstance].scopes = [[GIDSignIn sharedInstance].scopes arrayByAddingObjectsFromArray:additionalScopes]; [[GIDSignIn sharedInstance] signIn]; } #pragma mark - GoogleSignIn delegates - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error { if (error) { //TODO: handle error } else { NSString *userId = user.userID; NSString *fullName = user.profile.name; NSString *email = user.profile.email; NSURL *imageURL = [user.profile imageURLWithDimension:1024]; NSString *accessToken = user.authentication.accessToken; //Use this access token in Google+ API calls. //TODO: do your stuff } } - (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error { // Perform any operations when the user disconnects from app here. } - (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]; } 

你的AppDelegate.m

 #import <GoogleSignIn/GoogleSignIn.h> //... @implementation AppDelegate //... - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //... return YES; } //For iOS 9.0 and later - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options { //... return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]; } //For your app to run on iOS 8 and older, - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation { //... return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:sourceApplication annotation:annotation]; } @end 

另外请确保您已经添加了您的REVERSED_CLIENT_ID的 URL Scheme,您可以在GoogleService-Info.plist中find它。


为了能够在Google+中获得您的连接列表,您需要在Google API控制台中为您的项目启用Google People APIGoogle+ API

然后,只需将HTTP请求发送到以下url: https//www.googleapis.com/plus/v1/people/me/people/visible? access_token = YOUR_ACCESS_TOKEN


要检查用户是否拥有Google+个人资料,只需将HTTP请求发送到以下url: https//www.googleapis.com/plus/v1/people/USER_ID? "isPlusUser" ,然后检查"isPlusUser"的值是"true""false"

在这里输入图像说明

一点奖金
示例项目