2014-11-05 252 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