2013-09-30 84 views
1

在我的應用程序中,我試圖實現Google帳戶訪問,並且當我初始化它的工作直到登錄會話。在這之後拋出在屏幕下面的錯誤出手Google OAuth錯誤-1001

enter image description hereenter image description here

這裏我的代碼 初始化和方法實現

static NSString *const kKeychainItemName =nil; 
NSString *kMyClientID = @"465568347336.apps.googleusercontent.com";  
NSString *kMyClientSecret = @"rKVsWXTlo3M8zqNfofkX0Xrl"; 
NSString *scope = @"https://www.googleapis.com/auth/userinfo.profile"; 

GTMOAuth2ViewControllerTouch *viewController; 
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope 
                  clientID:kMyClientID 
                 clientSecret:kMyClientSecret 
                keychainItemName:kKeychainItemName 
                  delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)]; 
[self.navigationController presentModalViewController:viewController animated:YES]; 

錯誤處理程序

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
    finishedWithAuth:(GTMOAuth2Authentication *)auth 
      error:(NSError *)error { 
if (error != nil) { 
    NSString *output=nil; 
    output = [error description]; 
    NSLog(@"output:%@",output); 
    UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert" 
                message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error] 
                delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:@"Try again", nil]; 
    fail.tag = 1; 

    [fail show]; 
    NSLog(@"Authentication failed!"); 
    } else { 
    UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert" 
                 message:[NSString stringWithFormat:@"Authentication succeeded!"] 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    success.tag = 2; 

    [success show]; 
    NSLog(@"Autzentication succeeded!"); 
    } 

如何解決這個問題。請幫我解決

回答

3

我使用下面的代碼實現了我的GTMOAuth2,它爲我工作,我希望它可以以某種方式幫助你。

- (GTMOAuth2Authentication *)authForGoogle 
{  
    NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL]; 

    NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob"; 

    _auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"Google" 
                 tokenURL:tokenURL 
                 redirectURI:redirectURI 
                 clientID:GoogleClientID 
                clientSecret:GoogleClientSecret]; 
    _auth.scope = @"https://www.googleapis.com/auth/userinfo.profile"; 
    return _auth; 
} 


- (void)signInToGoogle 
{ 
    _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]; 
} 

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth 
     error:(NSError *)error 
{ 
    if (error != nil) { 
     NSString *output=nil; 
     output = [error description]; 
     NSLog(@"output:%@",output); 
     UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert" 
               message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error] 
               delegate:self 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:@"Try again", nil]; 
     fail.tag = 1; 

     [fail show]; 
     NSLog(@"Authentication failed!"); 
    } else { 
     UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert" 
                message:[NSString stringWithFormat:@"Authentication succeeded!"] 
               delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
     success.tag = 2; 

     [success show]; 
     NSLog(@"Autzentication succeeded!"); 
    } 
} 

我複製了您的finishedWithAuth方法,因爲它與我的實現類似。我認爲我們的代碼與xcode中GTMOAuth2的實現沒有太大區別,但我在使用GTMOAuth2時意識到的一件事情是,調試您所面臨的任何錯誤是非常困難的。我也有類似的錯誤,並且意識到這是因爲我在設置應用程序並獲取clientID和clientSecret時在Google門戶中選擇了錯誤類型的應用程序。我首先將它設置爲一個iOS應用程序(當然!),但在閱讀了不同的答案和在線問題後,我意識到我應該將它創建爲其他應用程序類型。這解決了我的問題。也許你可以查看它。

他們有一個論壇,支持還是比較全面的,這是鏈接here

爲了進一步增加,我可以把你介紹給我時,我被整合到GTMOAuth2我的應用程序所引用的教程。這是鏈接here

此外,由於我正在開發一個企業應用程序,要求我檢查用戶的電子郵件地址,因此即使在用戶通過身份驗證後,我也很難獲取電子郵件。在我得到我需要的東西之前,我必須破解並閱讀代碼,如果將來需要它,可以查看我的答案here on SO

希望這會有所幫助! :)

0

Error Domain=com.google.GTMOAuth2 Code=-1001通常會在用戶登錄時發生,但在OAuth協議窗口中(例如,它表示「想要訪問您的電子郵件」)用戶單擊「取消」或「不,謝謝」。

所以基本上沒有辦法「解決」這個。您可以處理它,或只是在沒有該服務的情況下在應用中取得進展