If you already received an access token ( GoogleTokenResponse ), you can also do this:
HttpTransport transport = new NetHttpTransport(); List<String> applicationScopes = Arrays.asList( PlusScopes.USERINFO_EMAIL, PlusScopes.USERINFO_PROFILE ); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( transport, JacksonFactory.getDefaultInstance(), "your-client-id.apps.googleusercontent.com", "your-client-secret", applicationScopes).build(); String userId = googleTokenResponse.parseIdToken().getPayload().getSubject(); Credential credential = flow.createAndStoreCredential(googleTokenResponse, userId); HttpRequestFactory requestFactory = transport.createRequestFactory(credential); GenericUrl url = new GenericUrl("https://www.googleapis.com/oauth2/v1/userinfo"); HttpRequest request = requestFactory.buildGetRequest(url); String userIdentity = request.execute().parseAsString();
userIdentity
will look like this:
{ "id": "105358994046791627189", "name": "Benny Neugebauer", "given_name": "Benny", "family_name": "Neugebauer", "link": "https://plus.google.com/+BennyNeugebauer", "picture": "https://lh4.googleusercontent.com/-dtvDIXCEtFc/AAAAAAAAAAI/AAAAAAAAAoE/1CKd3nH9rRo/photo.jpg", "gender": "male", "locale": "de" }
If you want, you can parse userIdentity
into your own class using Jackson:
ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper(); mapper.readValue(userIdentity, YourUser.class);
Here are the dependencies that I used for this example:
<dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-plus</artifactId> <version>v1-rev401-1.22.0</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> <type>jar</type> </dependency>
source share