Google Plus iOS SDK:如何登录用户电子邮件?

时间:2012-08-22 15:33:13

标签: objective-c ios google-plus

目前正在使用范围为:

的GooglePlusSample
@"https://www.googleapis.com/auth/plus.me", 
@"https://www.googleapis.com/auth/userinfo.email" and 
@"https://www.googleapis.com/auth/userinfo.profile". 

尝试在回调方法auth.userEmail中调用finishedWithAuth:error:,auth.userData,但两者都是空的......

4 个答案:

答案 0 :(得分:11)

-(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];

    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];


    GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];

    // *4. 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);

                }
            }];
    }

}

答案 1 :(得分:4)

用户通过身份验证后,您可以致电[[GPPSignIn sharedInstance] userEmail]以获取经过身份验证的用户的电子邮件。

答案 2 :(得分:0)

这对我有用:

首先使用userinfo.email范围:

signInButton.scope = [NSArray arrayWithObjects:
                      kGTLAuthScopePlusMe,
                      kGTLAuthScopePlusUserinfoEmail,
                      nil];

然后定义这些方法:

- (GTLServicePlus *)plusService {  
  static GTLServicePlus* service = nil;  
  if (!service) {
    service = [[GTLServicePlus alloc] init];
    // Have the service object set tickets to retry temporary error conditions
    // automatically
    service.retryEnabled = YES;
    // Have the service object set tickets to automatically fetch additional
    // pages of feeds when the feed's maxResult value is less than the number
    // of items in the feed
    service.shouldFetchNextPages = YES;
  }
  return service;
}

- (void)fetchUserProfile {
  // Make a batch for fetching both the user's profile and the activity feed
  GTLQueryPlus *profileQuery = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];
  profileQuery.fields = @"id,emails,image,name,displayName";
  profileQuery.completionBlock = ^(GTLServiceTicket *ticket, id object, NSError *error) {
    if (error == nil) {
      // Get the user profile
      GTLPlusPerson *userProfile = object;
      // Get what we want
      NSArray * userEmails = userProfile.emails;
      NSString * email = ((GTLPlusPersonEmailsItem *)[userEmails objectAtIndex:0]).value;
      NSString * name = userProfile.displayName;
      NSString * profileId = userProfile.identifier;
    } else {
      // Log the error
      NSLog(@"Error : %@", [error localizedDescription]);
    }
  };

  GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery];
  [batchQuery addQuery:profileQuery];
  GTLServicePlus *service = self.plusService;
  self.profileTicket = [service executeQuery:batchQuery
                           completionHandler:^(GTLServiceTicket *ticket,
                                               id result, NSError *error) {
                             self.profileTicket = nil;
                             // Update profile
                           }];
}

最后按照“finishedWithAuth”调用“fetchUserProfile”方法:

- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                   error: (NSError *) error
{
  // An error?
  if (error != nil) {
    // Log
  } else {
    // Set auth into the app delegate
    myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.auth = auth;  
    // Get user profile
    self.plusService.authorizer = auth;
    [self fetchUserProfile];
  }
}

请注意,这可能并不完美,因为它是“正在进行中的工作”,特别是:当用户拥有多个电子邮件地址时,获取正确的电子邮件地址,但这只是一个开始!

祝你好运。 史蒂夫

答案 3 :(得分:0)

如果您在google api控制台中具有Access not configured错误检查服务。确保启用google plus api服务。

相关问题