Google Plus登录

Nis*_*shi 14 iphone google-login access-token ios google-plus

场景:通过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);
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication:(NSString *)sourceApplication annotation: (id)annotation在AppDelegate中处理.

问题:

  • 当用户点击Google SignIn按钮并导航到Safari时,Google+登录功能完全正常.我的应用中收到了有效的AccessToken.
  • 但是,如果iPhone上有Google+应用,则用户会导航至Google+应用,但在登录时未收到任何AccessToken.我收到一个错误.

错误:

收到错误错误域= com.google.GooglePlusPlatform代码= -1"无法完成操作.(com.google.HTTPStatus错误400.)"UserInfo = 0x15d95f90 {NSLocalizedDescription =操作无法完成.(com.google.HTTPStatus错误400.)}和auth对象(null)

如果有人可以指导我解决这个问题,将会非常有帮助.谢谢.

Nee*_*enu 3

请在项目中设置 URL 方案。

脚步:

  • 登录您的开发者帐户
  • 选择您的项目
  • 从侧边栏选择 API 和身份验证
  • 选择凭证
  • 复制“REDIRECT URIS”和“BUNDLE ID”
  • Oen Xcode 项目
  • 选择项目目标
  • 选择“信息”
  • 扩展 URL 类型
  • 将“BUNDLE ID”粘贴到标识符区域
  • 将“REDIRECT URIS”粘贴到 URL 方案区域

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

希望这可以帮助你