使用GTMOAuth2在登录时检索Gmail地址 [英] retrieve gmail address on login using GTMOAuth2

查看:85
本文介绍了使用GTMOAuth2在登录时检索Gmail地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GTMOAuth2通过我的企业应用程序在iOS上登录.情况只有拥有@ mycompanyname.com帐户的用户才能登录.

I am using GTMOAuth2 for login on iOS with my enterprise app. Scenario is only users with account @mycompanyname.com can sign in.

问题是我正在调用文档中提供的方法,但是我无法检索他用于登录的用户的电子邮件帐户.

The problem is I am calling the method that is provided by the documentation, but I am unable to retrieve the user's email account that he used to sign in.

登录呼叫的步骤如下:

_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委托方法中,我这样做是为了尝试检索电子邮件地址:

In my delegate method of the GTMOAuth2 I did this to try to retrieve the email address:

- (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
}

}

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

The problem is that I cannot get the correct email address of the user who is trying to log in to be able to check accurately the email address and only initialize the app after.

我已经检查了错误,并全部使用@ gmail.com地址登录,这解释了为什么代码很长.

I have done the checks with the errors and all using @gmail.com address to login, which explains why the codes are long.

请帮助!预先感谢!

推荐答案

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

I have encountered the exact same issue!

经过数小时的研究,GTMOAuth2代码并记录了所有内容并跟踪了在此过程中调用的方法,我设法使它开始工作!

After hours and hours of looking into the GTMOAuth2 codes and logging everything and tracing the methods that were called along the way, I managed to get it to work!

我最终要检索的电子邮件地址是在方法下入侵GoogleOAuth类GTMOAuth2SignIn.m:

What I did eventually to retrieve the email address was to hack into the GoogleOAuth class GTMOAuth2SignIn.m under method:

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

这是因为认证将显示错误,这将导致认证对象无法检索已认证用户的值. IE.不会提取用户的电子邮件地址.

This is because the authentication will show an error, which will result in the authenthication object not retrieving the values of the authenticated user. ie. not pulling the email address of the user.

因此,我在方法中添加了一行,现在它显示如下:

Therefore I added a line to the method and now it shows as this:

- (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
}

之后,我使用了相同的呼叫

After that, I used the same call

[_auth userEmail]

并能够获取经过身份验证的用户的电子邮件地址.

and was able to get the email address of the authenticated user.

对我来说,这是一个漫长而痛苦的拔发调试会话,所以我希望这可以节省您一些时间,也可以节省大量头发! :)

It was a long, painful and hair pulling debugging session for me, so I hope this saved you some time and plenty of hair as well! :)

这篇关于使用GTMOAuth2在登录时检索Gmail地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆