ios - 谷歌加登录

标签 ios iphone google-plus access-token google-signin

场景:通过 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 中处理。

问题:

  • 当用户点击 Google 登录按钮并导航到 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)

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

最佳答案

请在项目中设置URL方案。

步骤:

  • Login your developer account
  • 选择您的项目
  • 从边栏中选择 API 和身份验证
  • 选择凭据
  • 复制“REDIRECT URIS”和“BUNDLE ID”
  • Oen Xcode 项目
  • 选择项目目标
  • 选择“信息”
  • 扩展 URL 类型
  • 在标识符区域粘贴“BUNDLE ID”
  • 将“REDIRECT URIS”粘贴到 URL Schemes 区域

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

希望对你有帮助

关于ios - 谷歌加登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26758725/

相关文章:

ios - Split View并独立为每个部分制作动画

ios - APN 状态 '400,' 如何获取有关错误的更多数据?

cordova - 在 PhoneGap 中使用 Googleplus 签名

Java Google Plus 通过 oAuth 2.0 访问

HTML 和 CSS : placing the Google +1 button makes it impossible to add space between elements

ios - 如果我返回 Swift,如何在 View 中移动而无需再次重新加载它们

ios - 函数产生预期类型 '(() -> ())?' ;你的意思是用 '()' 来调用它吗?

iphone - xcode 4 保持项目中的文件夹与模拟器同步的奇怪之处。

iphone - UIScrollView - 防止作为 UIScrollView subview 的 UIImageView 被拖出屏幕

ios - 在 iOS 中从 Facebook 获取个人资料照片