5

I'm developing an module that use GoogleAccountCredential to login, upload & download a file to GoogleDrive.

I want to get user first, last name and avatar of google account for display on my login feature.

I've try

 GoogleAccountCredential.getAccountName()

But It return only account name.

And see about OAuth 2.0 but not sure It can provide which I needed.

Don't know where to get those infomation, any suggestion also help me. It's awsome if have some examples

4

3 回答 3

11

ianhanniballake 的回答有效,但有更好的方法。您无需登录 Google+ 即可获取用户信息。

授权范围https://www.googleapis.com/auth/userinfo.profile

发出 GET 请求https://www.googleapis.com/oauth2/v1/userinfo?alt=json

你会得到

{
 "id": "xx",
 "name": "xx",
 "given_name": "xx",
 "family_name": "xx",
 "link": "xx",
 "picture": "xx",
 "gender": "xx",
 "locale": "xx"
}

以下文档中还有特定于语言的工作代码:Retrieving and Using OAuth 2.0 Credentials

玩得开心!

于 2013-07-28T05:48:32.600 回答
4

要获取此信息,您必须使用Google+ Sign In,它会在成功登录时为您提供一个PlusClient对象。然后,您可以使用PlusClient.getCurrentPerson检索Person(具有getName()getImage()方法)并使用 PlusClient.getAccountName()来获取accountName,您可以像使用过一样使用它GoogleAccountCredential

String accessToken = GoogleAuthUtil.getToken(context, accountName, OAUTH2_SCOPE);
final GoogleCredential credential = new GoogleCredential();
credential.setAccessToken(accessToken);
Drive driveConnection = new Drive.Builder(AndroidHttp.newCompatibleTransport(), 
    new GsonFactory(), credential).setApplicationName(APP_NAME).build();
于 2013-07-28T05:07:24.130 回答
2

看起来 Google 已弃用 JunYoung 提到的用户信息端点,转而支持 Google+ 登录。他们将在 2014 年 8 月之前停止支持 userinfo 端点。有关详细信息,请参阅下面的链接:

https://developers.google.com/+/api/auth-migration

于 2014-05-01T17:48:15.283 回答