【发布时间】:2012-09-05 22:32:04
【问题描述】:
我想为authentication 访问用户的Google 帐户user_id,但我在AccountManager 中没有看到任何提及。
我的应用如何请求user_id?
【问题讨论】:
标签: android oauth openid accountmanager
我想为authentication 访问用户的Google 帐户user_id,但我在AccountManager 中没有看到任何提及。
我的应用如何请求user_id?
【问题讨论】:
标签: android oauth openid accountmanager
现在Google Play Services 可用,您可以使用它来请求用户访问https://www.googleapis.com/auth/userinfo.profile 范围的权限,并使用生成的访问令牌向https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} 发出请求以获取他们的用户ID。
【讨论】:
userinfo?access_token={accessToken} 请求是否记录在某处?
Note: Google Play services has not launched, but we want to give you a quick overview of what to expect. 这是否意味着 API 尚不可用,或者它处于某种测试状态?
在 Google Play 服务发布之前,您需要使用 Android AccountManager 的 getAuthToken API。
这是一个如何使用 getAuthToken 获取access_token 的示例:https://stackoverflow.com/a/10988589/313790
【讨论】:
查看 Google 的 AccountManager example for the Tasks API。
获得访问令牌后,您就可以使用 google-api-java-client 的 oauth2 library 来请求 Userinfo 对象,而不是使用任务库,就像在 this example 中一样:
Oauth2 oauth2 = new Oauth2.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("Google-OAuth2Sample/1.0").build();
Userinfo userinfo = oauth2.userinfo().get().execute();
String userId = userinfo.getId();
【讨论】: