Iphone 使用Oauth2成功进行身份验证后获取用户信息
在我的iPhone应用程序中,我正在使用google登录,使用的是Iphone 使用Oauth2成功进行身份验证后获取用户信息,iphone,ios,oauth-2.0,google-oauth,Iphone,Ios,Oauth 2.0,Google Oauth,在我的iPhone应用程序中,我正在使用google登录,使用的是Oauth2,我正在跟踪并成功登录 - (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController finishedWithAuth:(GTMOAuth2Authentication * )auth error:(NSError * )error { if(!error) { UIAler
Oauth2
,我正在跟踪并成功登录
- (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController
finishedWithAuth:(GTMOAuth2Authentication * )auth
error:(NSError * )error
{
if(!error)
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success Authorizing with Google"
message:nil
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
我正在使用范围进行GTMOAuth2Authentication
,现在我想获得用户的基本信息,如姓名、年龄、电子邮件等
那么,我如何才能获得所有细节??我找了很多东西,但什么也没找到 可能是重复的问题,但也没有帮助 请提供帮助默认情况下,GTMOAuth2ViewControllerTouch viewController将获取用户的电子邮件,但不会获取用户配置文件的其余部分。
通过在登录前设置此属性,可以从谷歌服务器请求完整配置文件:
GTMOAuth2ViewControllerTouch*viewController代码>
viewController.signIn.shouldFetchGoogleUserProfile=YES代码>
以身份登录后,配置文件将可用
要获取其他信息,您必须更改范围
字符串并再次开始获取
下面是一些作用域URL
@”https://www.googleapis.com/auth/plus.me“
@”https://www.googleapis.com/auth/userinfo.email“
@”https://www.googleapis.com/auth/tasks“
默认情况下,GTMOAuth2ViewControllerTouch viewController将获取用户的电子邮件,但不会获取用户配置文件的其余部分。
通过在登录前设置此属性,可以从谷歌服务器请求完整配置文件:
GTMOAuth2ViewControllerTouch*viewController代码>
viewController.signIn.shouldFetchGoogleUserProfile=YES代码>
以身份登录后,配置文件将可用
要获取其他信息,您必须更改范围
字符串并再次开始获取
下面是一些作用域URL
@”https://www.googleapis.com/auth/plus.me“
@”https://www.googleapis.com/auth/userinfo.email“
@”https://www.googleapis.com/auth/tasks“
使用此代码解决了此问题
NSURL *url = [NSURL URLWithString:@"https://www.googleapis.com/plus/v1/people/me/activities/public"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[self.auth authorizeRequest:request
completionHandler:^(NSError *error) {
NSString *output = nil;
if (error) {
output = [error description];
} else {
// Synchronous fetches like this are a really bad idea in Cocoa applications
//
// For a very easy async alternative, we could use GTMHTTPFetcher
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (data) {
// API fetch succeeded
output = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] ;
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSArray *array=[json objectForKey:@"items"];
if (array.count>0) {
NSDictionary *dicts=[array objectAtIndex:0];
// NSLog(@"actor:%@",[dicts objectForKey:@"actor"]);
//[self updateUI:[dicts objectForKey:@"actor"]];
// [dictUserInfo setObject:[[[dicts objectForKey:@"actor"] objectForKey:@"image"]objectForKey:@"url"] forKey:@"imageUrl"];
if([delegate respondsToSelector:@selector(userPicChanged:)])
{
//send the delegate function with the amount entered by the user
[delegate userPicChanged:[[[dicts objectForKey:@"actor"] objectForKey:@"image"]objectForKey:@"url"]];
}
}
} else {
// fetch failed
output = [error description];
}
}
}];
用这个代码解决了它
NSURL *url = [NSURL URLWithString:@"https://www.googleapis.com/plus/v1/people/me/activities/public"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[self.auth authorizeRequest:request
completionHandler:^(NSError *error) {
NSString *output = nil;
if (error) {
output = [error description];
} else {
// Synchronous fetches like this are a really bad idea in Cocoa applications
//
// For a very easy async alternative, we could use GTMHTTPFetcher
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (data) {
// API fetch succeeded
output = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] ;
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSArray *array=[json objectForKey:@"items"];
if (array.count>0) {
NSDictionary *dicts=[array objectAtIndex:0];
// NSLog(@"actor:%@",[dicts objectForKey:@"actor"]);
//[self updateUI:[dicts objectForKey:@"actor"]];
// [dictUserInfo setObject:[[[dicts objectForKey:@"actor"] objectForKey:@"image"]objectForKey:@"url"] forKey:@"imageUrl"];
if([delegate respondsToSelector:@selector(userPicChanged:)])
{
//send the delegate function with the amount entered by the user
[delegate userPicChanged:[[[dicts objectForKey:@"actor"] objectForKey:@"image"]objectForKey:@"url"]];
}
}
} else {
// fetch failed
output = [error description];
}
}
}];
您必须将viewController的signIn.shouldFetchGoogleUserProfile设置为YES<代码>viewController.signIn.shouldFetchGoogleUserProfile=是代码>请检查我的答案,如果不起作用请告诉我@为此,您必须将viewController的signIn.shouldFetchGoogleUserProfile设置为YES<代码>viewController.signIn.shouldFetchGoogleUserProfile=是代码>请检查我的答案,如果不起作用请告诉我@托萨坎,你能展示一下你的尝试吗?我必须说,我还有另外一个问题,SDK显示“知道你在谷歌上是谁”。这是我的用户不想看到的。所以,同样的修复效果很好。非常感谢+1您能展示一下您的尝试吗?我必须说,我还有另外一个问题,SDK显示“知道你在谷歌上是谁”。这是我的用户不想看到的。所以,同样的修复效果很好。非常感谢