0

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

- (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 地址登录,这解释了为什么代码很长。

请帮忙!提前致谢!

4

2 回答 2

1

尝试这个 。. . .

- (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;
}
于 2013-10-23T07:37:13.433 回答
1

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

经过数小时的研究 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]

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

对我来说,这是一个漫长、痛苦和拉扯头发的调试过程,所以我希望这能为你节省一些时间和大量的头发!:)

于 2013-10-23T07:20:59.387 回答