Ios Google Plus登录

Ios Google Plus登录,ios,iphone,google-plus,access-token,google-login,Ios,Iphone,Google Plus,Access Token,Google Login,场景:通过Google+登录登录应用程序 实施: - (void)viewDidLoad { [super viewDidLoad]; GPPSignIn *aGPSSignIn = [GPPSignIn sharedInstance]; aGPSSignIn.scopes = @[ kGTLAuthScopePlusLogin, kGTLAuthScopePlusUserinfoEmail, kGTLAuthScopePlusUserinfoProfile, kGTL

场景:通过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)应用程序:(UIApplication*)应用程序openURL:(NSURL*)url
sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
在AppDelegate中处理

问题:

  • 当用户点击GoogleSignin按钮并被导航到Safari时,Google+登录可以很好地工作。在我的应用程序中收到有效的AccessToken
  • 但是,如果iPhone上有Google+应用程序,则会将用户导航到Google+应用程序,但登录时不会收到AccessToken。我犯了一个错误
错误:

- (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);
    }
}
收到的错误域=com.google.GooglePlusPlatform代码=-1“的 无法完成操作。(com.google.HTTPStatus错误400)。” UserInfo=0x15d95f90{NSLocalizedDescription=无法执行该操作 已完成。(com.google.HTTPStatus错误400。)和身份验证对象(空)

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

请在project中设置URL方案

步骤:

  • 选择您的项目
  • 从侧边栏中选择API和身份验证
  • 选择凭据
  • 复制“重定向URI”和“包ID”
  • Oen Xcode项目
  • 选择项目目标
  • 选择“信息”
  • 展开URL类型
  • 在标识符区域粘贴“BUNDLE ID”
  • 在URL方案区域粘贴“重定向URI”
如果不起作用,请尝试将“重定向URI”替换为“BUNDLE ID”,即标识符和URL方案相同


希望这对您有所帮助

您能否尝试将代码放入app delegate中的此函数中

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
并对该函数进行注释,然后告诉我结果

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

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

您是否已将应用程序包id放在Google开发者控制台上?这是从Google+API中的WebView或Safari启动应用程序的唯一方法。是的,我这样做了。但错误仍然是一样的。@Nishi您有什么解决方案吗?我遇到了同样的问题,我尝试用“BUNDLE ID”替换重定向URI,不幸的是它没有起作用。当我用“-(BOOL)application:(UIApplication*)application openURL:(NSURL*)url替换以前的方法时,我得到的令牌响应如下:“tokenResponse?e=20操作%20无法%E2%80%99t%20将%20完成。%20%28com.google.HTTPStatus%20错误%20400。%29&code=”。