我使用 GTMOAuth2 通过我的企业应用在 iOS 上登录。场景是只有帐户为@mycompanyname.com 的用户才能登录。
问题是我正在调用文档提供的方法,但我无法检索用户用于登录的电子邮件帐户。
登录调用如下:
_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 委托(delegate)方法中,我这样做是为了尝试检索电子邮件地址:
- (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
}
问题是我无法获得正在尝试登录的用户的正确电子邮件地址,以便能够准确检查电子邮件地址,并且只能在之后初始化应用程序。
我已经检查了错误并使用@gmail.com 地址登录,这解释了为什么代码很长。
求助!提前致谢!
最佳答案
我遇到了完全相同的问题!
经过数小时研究 GTMOAuth2 代码并记录所有内容并跟踪沿途调用的方法后,我设法让它工作了!
我最终检索电子邮件地址所做的是在方法下侵入 GoogleOAuth 类 GTMOAuth2SignIn.m:
- (void)auth:(GTMOAuth2Authentication *)auth finishedWithFetcher:(GTMHTTPFetcher *)fetcher error:(NSError *)error
这是因为身份验证会显示错误,这将导致身份验证对象无法检索已通过身份验证的用户的值。 IE。不提取用户的电子邮件地址。
因此我在方法中添加了一行,现在它显示如下:
- (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]
并且能够获取经过身份验证的用户的电子邮件地址。
这对我来说是一个漫长、痛苦且令人毛骨悚然的调试过程,所以我希望这能为您节省一些时间,也能节省大量的头发! :)
关于ios - 使用 GTMOAuth2 在登录时检索 gmail 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19535108/