2013-01-11 84 views
0

我如何使用oauth2.0获取用户在Android应用上的谷歌资料信息。我需要一个适当的代码/例如一个Android应用程序。我需要一个像用户的信息:oauth2.0用户资料信息

  1. 资料照片
  2. 生日
  3. 性别
  4. 位置

感谢

+0

嘿,如果你这样做了,可以请你分享,你是怎么想出这个。 – Ravi

回答

1

谷歌的OAuth将需要以下步骤:

1.注册Google here。注册后,在INSTALLED APPLICATION节你会得到你REDIRECT_URICLIENT_ID

2. REDIRECT_URI及以上现在获得CLIENT_ID将在下面的网址来使用。

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

4.现在,作为对此的回应,您将从Google获取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.Make使用访问令牌,使下面的网址呼叫

https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=your_access_token_here 

8.You将获得用户数据,该呼叫作为JSON的响应。

以下是你可能需要额外的文档: https://developers.google.com/accounts/docs/OAuth2InstalledApp

+0

这怎么可以使用GoogleAuthUtil.getToken完成? –

+0

我想了很久以来...但不知道如何编码。我正尝试使用GoogleAuthUtil.getToken() –

+1

非常感谢你!我的步骤帮助我完成了任务。我使用了GoogleAuthUtil。没有太多的文件可用于此。我会尽快为他人提供我的代码。 :) –

相关问题