我希望使用 oauth2 协议(protocol)对我的 android 应用程序的用户进行身份验证,这样用户就不需要为我的应用程序创建登录名和密码。
为此,我遵循以下教程:http://android-developers.blogspot.fr/2013/01/verifying-back-end-calls-from-android.html
我导入了 google play 服务 sdk 示例(在 AndroidSDK\extras\google\google_play_services\samples\auth 中),并且更改了范围值。
但是,我无法获得身份验证 token ...虽然我已经正确配置了我的 Google 控制台 API。
这是调用 getToken 方法的函数:
protected String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch (GooglePlayServicesAvailabilityException playEx) {
// GooglePlayServices.apk is either old, disabled, or not present.
mActivity.showErrorDialog(playEx.getConnectionStatusCode());
} catch (UserRecoverableAuthException userRecoverableException) {
// Unable to authenticate, but the user can fix this.
// Forward the user to the appropriate activity.
mActivity.startActivityForResult(userRecoverableException.getIntent(), mRequestCode);
} catch (GoogleAuthException fatalException) {
onError("Unrecoverable error " + fatalException.getMessage(), fatalException);
}
return null;
}
这是我的范围:audience:server:client_id:MY_CLIENT_ID.apps.googleusercontent.com
当我尝试这样做时,出现以下异常:
08-16 17:22:08.471: E/TokenInfoTask(16546): Exception:
08-16 17:22:08.471: E/TokenInfoTask(16546): com.google.android.gms.auth.GoogleAuthException: Unknown
08-16 17:22:08.471: E/TokenInfoTask(16546): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
最佳答案
您应该使用 Google 在 2 月份发布的 Google+ 登录,因为这将使您获得跨客户端授权并访问更多可用于个性化您的应用程序的数据。在幕后,此授权库使用 OAuth 2 连接到 Google 服务。
一些帮助您入门的资源:
- Google+ Android Quick Start应该可以帮助您进行基本实现。
- 如果您有任何问题,最常见的问题已在 Risk Complete Failure 博文中介绍 Common Problems with Sign-in on Android
- 如果您想查看完整的前端到后端集成,可以查看PhotoHunt。
- Google+ tag on Stack Overflow tag也有最新的信息和支持
关于android - Google play服务认证(oauth 2.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18277214/