Google OAuth错误-1001
在我的应用程序中,我尝试实施谷歌帐户访问,并在我初始化其工作直到登录会话。之后它会在屏幕截图中抛出以下错误
In my application i have try to implement the google account access, and when i initialise its working till in the login session. after which it throws the following error in the screen shot
这里我的代码
初始化和方法实现
static NSString *const kKeychainItemName =nil;
NSString *kMyClientID = @"465568347336.apps.googleusercontent.com";
NSString *kMyClientSecret = @"rKVsWXTlo3M8zqNfofkX0Xrl";
NSString *scope = @"https://www.googleapis.com/auth/userinfo.profile";
GTMOAuth2ViewControllerTouch *viewController;
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
clientID:kMyClientID
clientSecret:kMyClientSecret
keychainItemName:kKeychainItemName
delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[self.navigationController presentModalViewController:viewController animated:YES];
错误处理程序
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error != nil) {
NSString *output=nil;
output = [error description];
NSLog(@"output:%@",output);
UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Try again", nil];
fail.tag = 1;
[fail show];
NSLog(@"Authentication failed!");
} else {
UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[NSString stringWithFormat:@"Authentication succeeded!"]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
success.tag = 2;
[success show];
NSLog(@"Autzentication succeeded!");
}
如何解决这个问题。请帮我解决
How to solve this is issue.Please help me to solve
我使用以下代码实现了我的GTMOAuth2,它对我有用,我希望它可以以某种方式帮助你。
I implemented my GTMOAuth2 using the following codes and it worked for me, I hope it may help you in some way or another.
- (GTMOAuth2Authentication * )authForGoogle
{
NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL];
NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob";
_auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"Google"
tokenURL:tokenURL
redirectURI:redirectURI
clientID:GoogleClientID
clientSecret:GoogleClientSecret];
_auth.scope = @"https://www.googleapis.com/auth/userinfo.profile";
return _auth;
}
- (void)signInToGoogle
{
_auth = [self authForGoogle];
// Display the authentication view
GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:_auth
authorizationURL:[NSURL URLWithString:GoogleAuthURL]
keychainItemName:@"GoogleKeychainName"
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[_window setRootViewController: viewController];
[_window makeKeyAndVisible];
}
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error
{
if (error != nil) {
NSString *output=nil;
output = [error description];
NSLog(@"output:%@",output);
UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Try again", nil];
fail.tag = 1;
[fail show];
NSLog(@"Authentication failed!");
} else {
UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[NSString stringWithFormat:@"Authentication succeeded!"]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
success.tag = 2;
[success show];
NSLog(@"Autzentication succeeded!");
}
}
我复制了你的finishedWithAuth方法,因为它类似于我的实现。我认为我们的代码与xcode中GTMOAuth2的实现没有太大区别,但我在使用GTMOAuth2时意识到的一件事是调试你遇到的任何错误都非常困难。我也有类似的错误,并意识到这是因为我在设置应用程序并获取clientID和clientSecret时在Google门户中选择了错误类型的应用程序。我首先将它设置为iOS应用程序(当然!),但是在线阅读了不同的答案和问题后,我意识到我应该将其创建为其他应用程序类型。这解决了我的问题。也许你可以看一下。
I copied your finishedWithAuth method as it is similar to my implementation. I don't think there is much difference between our codes with regards to the implementation of GTMOAuth2 in xcode, but one thing that I realised when I was using GTMOAuth2 was that it is extremely hard to debug any errors that you face. I had this similar error to you as well, and realised that it was because I chose the wrong type of app in the Google portal when setting up the app and getting the clientID and clientSecret. I set it up as an iOS app at first (of course!), but then after reading different answers and problems online, I realised I should have created it as an Other app type. That solved my issue. Maybe you can check it out.
他们有一个论坛,支持非常全面,这是链接这里
They have a forum where the support is quite comprehensive, this is the link here
要进一步添加,我可以向您推荐我在将GTMOAuth2集成到我的应用程序时引用的教程。这是此处的链接。
To further add on, I can refer you to the tutorial that I referenced when I was integrating the GTMOAuth2 into my app. This is the link here.
此外,由于我正在开发一个企业应用程序,要求我检查用户的电子邮件地址,即使在用户通过身份验证后,我也很难收到电子邮件。在得到我需要的东西之前,我不得不破解并阅读代码,如果将来需要它,你可以查看我的答案这里就这样了。
Also, as I was developing an enterprise app that required me to check the email address of the user, it was very difficult for me to get the email even after the user was authenticated. I had to hack and read into the codes before I got what I needed, and if you need it in future, you can check my answer here on SO.
希望这会有所帮助! :)
Hope this helps! :)