Google Plus Login



场景:通过Google+登录登录应用程序

实施:

- (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
  • 然而,如果iPhone上有Google+应用程序,则用户会被导航到Google+应用,但在登录时不会收到AccessToken。我收到一个错误

错误:

收到错误错误Domain=com.google.GooglePlusPlatform Code=-1"操作无法完成。(com.google.HTTPStatus错误400。)"UserInfo=0x15d95f90{NSLocalizedDescription=无法执行操作已完成。(com.google.HTTPStatus错误400.)}和身份验证对象(null)

如果有人能指导我解决这个问题,那将是非常有帮助的。谢谢

请在项目中设置URL方案。

步骤:

  • 登录您的开发者帐户
  • 选择您的项目
  • 选择API&侧边栏的身份验证
  • 选择凭据
  • 复制"REDIRECT URIS"one_answers"BUNDLE ID"
  • Oen Xcode项目
  • 选择项目目标
  • 选择"信息"
  • 展开URL类型
  • 在标识符区域粘贴"BUNDLE ID"
  • 在URL方案区域粘贴"REDIRECT URIS"

如果不起作用,请尝试将"REDIRECT URIS"替换为"BUNDLE ID",即标识符和URL方案相同。

希望这能帮助您

你能试着把你的代码放在应用程序代理中的这个函数中吗

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

并评论这个函数并告诉我的结果

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

sourceApplication:(NSString*)sourceApplication annotation:(id)annotation

最新更新