How to get user profile on Google API using the JAVA library?
Asked Answered
S

2

5

I have a Java ClientRequest to the Google API that returns a JSON containing the profile based on an access_token. The URL is:
https://www.googleapis.com/oauth2/v1/userinfo?access_token=ya29.1.AADtN_VALuuUTBm8ENfNTz8s...

And the response is:

{
    "id": "111223344556677889900",
    "email": "[email protected]",
    "verified_email": true,
    "name": "My Name",
    "given_name": "My",
    "family_name": "Name",
    "link": "plus.google.com/111223344556677889900",
    "picture": "photo.jpg",
    "gender": "male",
    "locale": "en"
}

Some points:

1 I want to use the Java library to avoid mounting the HTTP request, keep the google server URL, and other minor things.
2 I don't need the authorization steps because, at this point, my method receives the access token (all the Oauth steps are done before).
3 In this method, we don't have (and so far don't need) the client id and secret.
4 I don't need the Google+ scope. Actually, I prefer not to go there. So far only found examples using the Plus library.

In summary, I need something in the google API Java library precisely equivalent to the HTTP request I use nowadays.

Thank you very much in advance!

Shilling answered 19/3, 2014 at 19:53 Comment(3)
It sounds like you are referring to Google+ API, in which case I suggest you modify the title and tags to reflect that.Held
Hi, Tom. Actually I wouldnt like to use the Google+ API. I can get the user basic information without G+ API just making a HTTP request to the Google API (not +). The point is: how to do the same using the Java lib (avoiding keeping the google api url).Shilling
Not sure i follow, but note that the google java client library includes an http library that is equivalent to java http library. On gae it wraps urlfetch. Either are suitable for formulating and executing simple http requests.Held
M
29

I hope this helps

GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);   
 Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName(
          "Oauth2").build();
 Userinfoplus userinfo = oauth2.userinfo().get().execute();
 userinfo.toPrettyString();
Mervin answered 27/3, 2014 at 11:51 Comment(6)
Helped a lot! Exactly what I need. Thank you very much @Mervin !Shilling
just complementing... I had to add the following dependency: <dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-oauth2</artifactId> <version>v2-rev65-1.17.0-rc</version> </dependency>Shilling
I got this working but the email always returning null, any idea?Ran
@geek you have to request the com.google.api.services.oauth2.Oauth2Scopes.USERINFO_EMAIL scope to access the email address.Dread
Where do I get the accessToken from?Lachrymose
In later versions of the library, use Userinfo rather than UserinfoPlusShari
S
2

Complementing with more info, since I don't have enough reputation to comment on the above answer:

After adding

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-oauth2</artifactId>     
    <version>v2-rev65-1.17.0-rc</version>
</dependency>

to your pom.xml file, the following line

Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName("Oauth2").build();

could raise "JacksonFactory cannot be resolved to a type", and if this happens, you can replace it with new GsonFactory() (from com.google.api.client.json.gson), and it'll work.

Now, about how to get an accessToken: it comes inside com.google.auth.oauth2.UserCredentials (com.google.auth.oauth2.UserCredentials), which you can get by calling com.google.auth.oauth2.UserAuthorizer.getCredentialsFromCode.

You can build a UserAuthorizer by calling

UserAuthorizer
    .newBuilder()
    .setClientId(ClientId.of(<client id>, <client secret>))
    .setScopes(<scopes>)
    .build()

You can get the <client id> and the <client secret> by reading the OAuth Client ID file created on your google cloud project dashboard under credentials.

And lastly, an example of how to read the file using com.google.auth.oauth2.ClientId:

ClientId parsedClient = ClientId.fromStream(new FileInputStream("key.json"))

I'm a bit late here, but I hope this helps someone.

Suannesuarez answered 13/9, 2022 at 1:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.