场景:通过 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/