0

エンタープライズ アプリで iOS にログインするために GTMOAuth2 を使用しています。シナリオは、アカウント @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

私はまったく同じ問題に遭遇しました!

何時間もかけて GTMOAuth2 コードを調べ、すべてをログに記録し、途中で呼び出されたメソッドをトレースした後、なんとか動作させることができました!

メールアドレスを取得するために最終的に行ったことは、メソッドの下で GoogleOAuth クラス GTMOAuth2SignIn.m にハッキングすることでした。

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

これは、認証でエラーが表示され、認証オブジェクトが認証されたユーザーの値を取得できないためです。すなわち。ユーザーのメールアドレスを取得していません。

したがって、メソッドに行を追加すると、次のように表示されます。

- (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 に答える