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 に答える
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 に答える