2014-11-05 249 views
14

場景:通過Google+登錄谷歌加登錄

實現登錄到App:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    GPPSignIn *aGPSSignIn = [GPPSignIn sharedInstance]; 
    aGPSSignIn.scopes = @[ kGTLAuthScopePlusLogin, kGTLAuthScopePlusUserinfoEmail, kGTLAuthScopePlusUserinfoProfile, kGTLAuthScopePlusMe]; 
    aGPSSignIn.shouldFetchGoogleUserEmail = YES; 
    aGPSSignIn.shouldFetchGooglePlusUser =YES; 
    aGPSSignIn.homeServerClientID = kClientID; 
    aGPSSignIn.clientID = kClientID; 
    aGPSSignIn.delegate = self; 
    if (![aGPSSignIn trySilentAuthentication]) { 
     [self showLoginButton]; 
    } 
} 

- (void)showLoginButton { 
    if (!self.signInButton) {   
     self.signInButton = [GPPSignInButton buttonWithType:UIButtonTypeCustom]; 
     [self.signInButton setFrame:CGRectMake(60, 100, 200, 40)]; 
    } 
    [self.view addSubview:self.signInButton]; 
} 

#pragma mark - GPPSignInDelegate 

- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error { 
    NSString *anAccessToken = auth.accessToken; 
    NSLog(@"googleAccessToken:%@",anAccessToken); 
} 

- (void)didDisconnectWithError:(NSError *)iError { 
    if (iError) { 
     NSLog(@"Error:%@", iError); 
    } 
} 

注:- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication:(NSString *)sourceApplication annotation: (id)annotation,在AppDelegate處理。

問題:

  • 當用戶點擊谷歌登入按鈕,並導航到Safari瀏覽器的Google+帳戶登錄工作完全正常。我的應用程序接收到有效的AccessToken。
  • 但是,如果Google+應用程序在iPhone上可用,用戶將被導航到Google+應用程序,但在登錄時不會收到AccessToken。我收到一個錯誤。

錯誤:

Received error Error Domain=com.google.GooglePlusPlatform Code=-1 "The operation couldn’t be completed. (com.google.HTTPStatus error 400.)" UserInfo=0x15d95f90 {NSLocalizedDescription=The operation couldn’t be completed. (com.google.HTTPStatus error 400.)} and auth object (null)

這將是非常有益的,如果有人能指導我解決這個問題。 謝謝。

+0

你把你的應用程序Google開發者控制檯上的套件ID?這是WebView或Safari API在Google+ API中啓動應用程序的唯一途徑。 – khunshan 2014-11-25 19:22:03

+0

是的,我做了。但仍然錯誤保持不變。 – Nishi 2014-11-26 10:48:40

+0

@Nishi你有什麼解決方案嗎?我面臨同樣的問題http://stackoverflow.com/questions/30160104/authentication-not-working-with-google-app – Tariq 2015-05-15 08:39:42

回答

3

請在項目中設置URL方案。

步驟:

  • Login your developer account
  • 從側邊欄選擇你的項目
  • 選擇的API &權威性
  • 選擇證書
  • 複製 '重定向URI' 和 '包ID'
  • Oen Xcode項目
  • 選擇項目目標
  • 選擇「信息」
  • 擴大URL類型
  • 粘貼在標識方面的「包ID」
  • 粘貼到URL方案領域的「重新導向URI」

如果它不工作嘗試通過用'BUNDLE ID'取代'REDIRECT URIS',即標識符和URL方案是相同的。

希望這可以幫助你

+0

我試圖用'BUNDLE ID'替換REDIRECT URIS',不幸的是它沒有工作。 – Nishi 2014-11-26 10:44:15

+0

Didnt working fr too – Tariq 2015-05-15 10:28:53

0

你可以嘗試把你的代碼在此功能在應用程序委託

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 

和評論此功能,並告訴我結果

- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url 

sourceApplication :(NSString *)sourceApplication註釋:(id)註釋

+0

當我用「 - (BOOL)應用程序:(UIApplication *)應用程序openURL:(NSURL *)url」替換之前的方法時,我得到的令牌響應如下:「tokenResponse?e =%20operation%20couldn %E2%80%99噸%20be%20completed。%20%28com.google.HTTPStatus%20error%20400.%29&代碼=」。 – Nishi 2014-11-26 10:46:53