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

Tai Dao
  • 3,407
  • 7
  • 31
  • 54

3 Answers3

11

ianhanniballake's answer works, but there is a better way to do it. You don't need Google+ sign in to get user info.

Authorize with scope https://www.googleapis.com/auth/userinfo.profile

Make GET request to https://www.googleapis.com/oauth2/v1/userinfo?alt=json

You will get

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

There are also language specific working codes in the following documentation: Retrieving and Using OAuth 2.0 Credentials.

Have fun!

JunYoung Gwak
  • 2,967
  • 3
  • 21
  • 35
  • Sorry for late reply, I don't want login using google sign in, just use google email ID (like abc@gmail.com) or google account added on the phone. Can I do that? I've tried your link but It return Invalid request. I tried with this call: "String res = MyClass.getUserInfo(MyClass.getCredentials(MyClass.getAuthorizationUrl("dolphin.testers@gmail.com", null), null)).getFamilyName();" – Tai Dao Jul 28 '13 at 12:52
  • Just get first, last name. I don't want avatar anymore – Tai Dao Jul 28 '13 at 13:06
  • 3
    User must be logged in and authorize you to access his/her user profile. Otherwise, it will be a serious privacy issue. – JunYoung Gwak Jul 29 '13 at 01:53
4

To get this information, you must use Google+ Sign In, which gives you a PlusClient object on successful login. You can then use PlusClient.getCurrentPerson to retrieve a Person (who has a getName() and getImage() method) and PlusClient.getAccountName() to get the accountName, which you would use in the same way as if you had used 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();
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • yes you MUST login,that's the only way to get the `PlusClient` object which you can then go on to use to get whatever information are available to your app.(with permission of course) – Ojonugwa Jude Ochalifu Feb 10 '14 at 14:27
2

Looks like Google has deprecated the userinfo endpoint that JunYoung mentioned in favor of the Google+ Sign-in. They will stop supporting the userinfo endpoint by August, 2014. See the link below for details:

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

Price
  • 2,683
  • 3
  • 17
  • 43