3

目前正在使用具有范围的 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在回调方法中调用 auth.userData finishedWithAuth:error:,但两者都是空的......

4

4 回答 4

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

                }
            }];
    }

}
于 2014-03-12T13:16:17.417 回答
4

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

于 2014-02-20T11:46:32.803 回答
0

如果您在 google api 控制台中未配置 Access 错误检查服务。确保启用 google plus api 服务。

于 2013-05-30T05:23:55.980 回答
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];
  }
}

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

祝你好运。史蒂夫

于 2012-08-24T21:02:34.913 回答