这对我有用:
首先使用 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];
}
}
请注意,这可能并不完美,因为它是“正在进行的工作”,特别是关于:当用户拥有多个电子邮件地址时获取正确的电子邮件地址,但这只是一个开始!
祝你好运。史蒂夫