使用Google+ iOS API如何登录用户的个人资料详情?

sle*_*rfx 10 objective-c ios google-plus

我已成功从iPhone应用程序成功登录google plus.但是如何登录用户的详细信息?如个人资料ID,电子邮件等

我试过这个, Stackoverflow回答了类似的问题,但我无法让它工作.在那个样本中,究竟是什么传递给accessTocken,

NSString *str =  [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",accessTocken];
Run Code Online (Sandbox Code Playgroud)

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

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

小智 34

- (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);
            }
        }];
    }
}
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助


Muh*_*din 14

这是获取当前登录用户的电子邮件ID的最简单和最简单的方法,首先创建GPPSignIn类的实例变量

GPPSignIn *signIn;
Run Code Online (Sandbox Code Playgroud)

然后在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;

 }
Run Code Online (Sandbox Code Playgroud)

接下来GPPSignInDelegate在您的视图控制器中实现您可以获取登录用户的电子邮件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
 }
Run Code Online (Sandbox Code Playgroud)