使用GTMOAuth2检索login时的Gmail地址

我用我的企业应用程序在iOS上使用GTMOAuth2进行login。 scheme只有拥有account @ mycompanyname.com的用户才能login。

问题是我打电话给文档提供的方法,但我无法检索用户的电子邮件帐户,他用来login。

呼叫login如下:

_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]; 

在我GTMOAuth2的委托方法中,我做了这个尝试检索电子邮件地址:

 - (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController finishedWithAuth:(GTMOAuth2Authentication * )auth error:(NSError * )error { if ([[_auth userEmail] rangeOfString:@"@mycompanyname.com"].location == NSNotFound && error != nil ) { // Loop for no mycompanyname mail domain and also error in login NSLog(@"Loop 1 - Non mycompanyname domain email OR Google login error."); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please Login with your mycompanyname email." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } else if ([[_auth userEmail] rangeOfString:@"@mycompanyname.com"].location == NSNotFound) { // Loop for no error in login but without mycompanyname mail domain NSLog(@"Loop 2 - Non mycompanyname domain email AND no Google login error."); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Authentication Denied" message:@"Please Login with your mycompanyname email." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } else if (error != nil) { NSLog(@"Loop 3 - mycompanyname domain email AND Google login error."); if ([[error localizedDescription] rangeOfString:@"error -1000"].location != NSNotFound) { NSLog(@"Loop 3.1 - Error message contains 'error -1000'."); // Loop to catch unwanted error message from GTMOAuth which will show if user enters the app and decides not to sign in but enters the app after again. } else { // Loop for error in authentication of user for google account NSLog(@"Loop 3.2 - Error message caused by no internet connection."); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your internet connection seems to be lost." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } } else { // Loop for mycompanyname mail domain and no authentication error NSLog(@"Loop 4 - mycompanyname domain email AND no Google login error."); // initialize app } 

}

问题是,我无法获得正在尝试login的用户的正确电子邮件地址,以便能够准确地检查电子邮件地址,并且只在之后初始化应用程序。

我已经做了错误的检查和所有使用@ gmail.com地址login,这解释了为什么代码很长。

请帮忙! 提前致谢!

我遇到了完全相同的问题!

在查看GTMOAuth2代码并logging所有事件并追踪所调用的方法之后,我花了数小时的时间,设法使其运行起来!

我最终取回电子邮件地址的方法是在方法下攻击GoogleOAuth类GTMOAuth2SignIn.m:

 - (void)auth:(GTMOAuth2Authentication *)auth finishedWithFetcher:(GTMHTTPFetcher *)fetcher error:(NSError *)error 

这是因为validation会显示错误,这将导致validation对象不检索validation用户的值。 即。 不拉用户的电子邮件地址。

因此,我在该方法中添加了一行,现在显示为:

 - (void)auth:(GTMOAuth2Authentication *)auth finishedWithFetcher:(GTMHTTPFetcher*)fetcher error:(NSError *)error { self.pendingFetcher = nil; #if !GTM_OAUTH2_SKIP_GOOGLE_SUPPORT if (error == nil && (self.shouldFetchGoogleUserEmail || self.shouldFetchGoogleUserProfile) && [self.authentication.serviceProvider isEqual:kGTMOAuth2ServiceProviderGoogle]) { // fetch the user's information from the Google server [self fetchGoogleUserInfo]; } else { // we're not authorizing with Google, so we're done /**** CHANGED HERE TO CALL THE FETCH METHOD NO MATTER WHAT SO THAT THE EMAIL CAN BE SHOWN *****/ [self fetchGoogleUserInfo]; /**********************************************************************************************/ //[self finishSignInWithError:error]; // comment this out so that it will it will initiate a successful login } #else [self finishSignInWithError:error]; #endif } 

之后,我用同样的电话

 [_auth userEmail] 

并能够获得authentication用户的电子邮件地址。

对我来说,这是一个漫长而痛苦的拉动debugging的过程,所以我希望能为你节省一些时间和大量的头发! 🙂

尝试这个 。 。 。 。

 - (void)signInWithGoogle:(id)sender { GTMOAuth2Authentication * auth = [self authForGoogle]; NSString * googleAuthURL = @"https://accounts.google.com/o/oauth2/auth"; GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth authorizationURL:[NSURL URLWithString:googleAuthURL] keychainItemName:@"GoogleKeychainName" completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) { if (error != nil) { //error } else { googleAuth = auth; NSMutableDictionary * parameters = auth.parameters; NSString * email = [parameters objectForKey:@"email"]; } }]; } } -(GTMOAuth2Authentication *) authForGoogle { NSString * googleTokenURL = token; NSString * googleClientID = client id; NSString * googleClientSecret = client secret; NSString * redirectURI = redirect uri; NSURL * tokenURL = [NSURL URLWithString:googleTokenURL]; GTMOAuth2Authentication * auth ; auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"Google" tokenURL:tokenURL redirectURI:redirectURI clientID:googleClientID clientSecret:googleClientSecret ]; auth.scope = scope; return auth; }