0

我如何使用 oauth2.0 在 Android 应用程序上获取用户的谷歌个人资料信息。我需要适用于 android 应用程序的正确代码/示例。我需要用户信息,例如:

  1. 头像照片
  2. 生日
  3. 性别
  4. 地点

谢谢

4

1 回答 1

1

Google OAuth 将需要以下步骤:

1.在这里注册谷歌。注册后,在INSTALLED APPLICATION部分您将获得您的REDIRECT_URICLIENT_ID

2.上面得到的REDIRECT_URICLIENT_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.此网址将带您进入谷歌身份验证页面,在这里谷歌接管并输入您的帐户详细信息。此外,它被重定向到批准页面,用户允许您的应用使用他们的谷歌数据。

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

于 2013-01-11T06:11:02.713 回答