Use a service account to get the list of users from Google domain
Asked Answered
N

0

7


Hello all.

I have been assigned the task of fetching unanswered emails from the inbox of each member of our Google domain using Spring Boot, but I haven't been able to do it.

In first place, I need the list of users from the domain. This can be achieved via Directory API (which cannot be enabled by that name in the Google Developer console, by the way. Looks like it belongs to Admin SDK or so).

What I have faced so far is this:

  • There are many related questions on SO, but most of them are outdated.
  • Java Quickstart for Google Directory API does not include an example using service accounts, and I want to use them because my app runs in a docker container, and using Oauth means I need to manually authorize it every time I deploy a new version or restart the container.
  • Google documentation makes reference to "API Reference" settings in Admin console, but I don't see that section there.

I am not storing credentials in a JSON file, I have them in an environment variable instead. I am doing this:

var inputStream = IOUtils.toInputStream(apiCredentials, Charset.defaultCharset());  //apiCredentials is a string with the JSON contents.
var credential = GoogleCredential
                .fromStream(inputStream, httpTransport, JacksonFactory.getDefaultInstance())
                .createScoped(Collections.singleton(DirectoryScopes.ADMIN_DIRECTORY_USER));
var directoryService = new Directory.Builder(httpTransport, JacksonFactory.getDefaultInstance(), credential)
                .setApplicationName("My App")
                .build();
var result = directoryService.users().list()
                    .setPageToken(pageToken)
                    .setDomain("my.domain")
                    .setMaxResults(10)
                    .execute();

After this, I get a 400 Bad request error, with no further description.

What am I doing wrong here?

Nagel answered 16/6, 2020 at 0:10 Comment(1)
See example, it work for mec: https://mcmap.net/q/1629341/-how-can-i-get-the-data-of-the-domain-users-using-a-service-accountLoveland

© 2022 - 2024 — McMap. All rights reserved.