Amar Premsaran Patel
Amar Premsaran Patel

Reputation: 1383

How do I set the scope to call the Google People API in Java?

I am using the following code. I have a valid accessToken. How can I set the scope to email,profile on the Credential? I am using OAuth2 for authorization.

        static final String APPLICATION_NAME = "calendar-service";
        static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

        Credential credential = new 
 Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessToken);
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    
        PeopleService service = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();

        ListConnectionsResponse response = service.people().connections()
                .list("people/me")
                .setPersonFields("names,emailAddresses")
                .execute();
        
        List<Person> connections = response.getConnections();
        if (connections != null && connections.size() > 0) {
            for (Person person : connections) {
                List<Name> names = person.getNames();
                if (names != null && names.size() > 0) {
                    System.out.println("Name: " + person.getNames().get(0)
                            .getDisplayName());
                } else {
                    System.out.println("No names available for connection.");
                }
            }
        } else {
            System.out.println("No connections found.");
        }
        
        return connections.get(0);

I am getting the following error:

GET https://people.googleapis.com/v1/people/me/connections?personFields=names,emailAddresses
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Request had insufficient authentication scopes.",
  "status" : "PERMISSION_DENIED"
}] with root cause

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden

Here is my application.properties:

spring.security.oauth2.client.registration.google.clientId=XYZ456.apps.googleusercontent.com
spring.security.oauth2.client.registration.google.clientSecret=ABC123
spring.security.oauth2.client.registration.google.redirectUri={baseUrl}/oauth2/callback/{registrationId}
spring.security.oauth2.client.registration.google.scope=email,profile

Please advise.

Upvotes: 2

Views: 2284

Answers (1)

ziganotschka
ziganotschka

Reputation: 26826

There is a quickstart for using People API with Java

It contains the following line:

private static final List<String> SCOPES = Arrays.asList(PeopleServiceScopes.CONTACTS_READONLY);

This is the line where you can change your scope to a ddiferent one if desired, or add additional scopes.

To know which scopes you need, go onto the documentation page for the method you use.

enter image description here

IMPORTANT

Each time you change the scopes, you need to delete the token file from your machine, so that new authorization flow willbe triggered.

Upvotes: 1

Related Questions