【发布时间】:2013-01-16 03:34:19
【问题描述】:
我如何使用 oauth2.0 在 android 应用程序上获取用户的 google 个人资料信息。我需要适用于 android 应用程序的正确代码/示例。我需要用户信息,例如:
- 个人资料照片
- 生日
- 性别
- 位置
谢谢
【问题讨论】:
-
嘿,如果你做过这个,能不能分享一下,你是怎么想到这个的。
标签: android oauth-2.0 google-oauth
我如何使用 oauth2.0 在 android 应用程序上获取用户的 google 个人资料信息。我需要适用于 android 应用程序的正确代码/示例。我需要用户信息,例如:
谢谢
【问题讨论】:
标签: android oauth-2.0 google-oauth
Google OAuth 需要以下步骤:
1.注册谷歌here。注册后,在INSTALLED APPLICATION部分你会得到你的REDIRECT_URI和CLIENT_ID
2.上面得到的REDIRECT_URI和CLIENT_ID现在将在下面的url中使用。
https://accounts.google.com/o/oauth2/auth?" + "scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile"+ "&redirect_uri=" + REDIRECT_URI + "&response_type=code" +"&client_id=" + CLIENT_ID;
3.此 url 将带您进入 Google 身份验证页面,在这里 google 接管并输入您的帐户详细信息。此外,它被重定向到批准页面,用户允许您的应用使用他们的谷歌数据。
4.现在,作为对此的回应,您会从谷歌获得ACCESS CODE 作为 JSON 或 html 页面的标题。解析ACCESS CODE。
5.使用ACCESS CODE,您现在将使用以下发布数据发出POST 请求以获取ACCESS TOKEN。
'code' // this is the access code
'client_id' // same as earlier
'client_secret' // you will find this on the google page where you registered
'redirect_uri' // same as earlier
'grant_type' = "authorization_code" // as is
6.您现在将以 JSON 格式获取 ACCESS TOKEN 作为“access_token”。解析此访问令牌。
7.使用访问令牌调用以下url
https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=your_access_token_here
8.您将以 JSON 格式获取用户数据作为对该调用的响应。
以下是您可能需要的其他文档: https://developers.google.com/accounts/docs/OAuth2InstalledApp
【讨论】: