0

There is a service account with domain-wide delegation and I need to get a list of users from Workspace by means of Admin SDK / Directory Api. Should I always call serviceAccountCredentials.createDelegated( delegatedUserEmail ) or similar ? This means I has to know at least one user email before getting the list of users (emails). Is there workaround for specifying this email?

        final ServiceAccountCredentials serviceAccountCredentials = ServiceAccountCredentials
                .fromPkcs8(
                        clientId,
                        clientEmail,
                        serviceAccountPkcs8Key,
                        serviceAccountPkcs8Id,
                        Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY));

        final GoogleCredentials delegatedCredentials = serviceAccountCredentials.createDelegated(delegatedUserEmail);

        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(delegatedCredentials);

        Directory directory = new Directory.Builder(
                httpTransport, JSON_FACTORY, requestInitializer)
                .setApplicationName(applicationName)
                .build();

if I replace

final GoogleCredentials delegatedCredentials = serviceAccountCredentials.createDelegated(delegatedUserEmail);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(delegatedCredentials);

with

HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
serviceAccountCredentials.createScoped(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY));

api responds an error:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
GET https://www.googleapis.com/admin/directory/v1/users?customer=my_customer&maxResults=500
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid Input",
    "reason" : "invalid"
  } ],
  "message" : "Invalid Input"
}
Denis
  • 759
  • 1
  • 9
  • 22
  • I don't think it's necessary, as the call to https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list doesn't require an email – Aerials Jan 25 '21 at 17:03
  • @Aerials it's correct that there is no email required, but if it not specified, it results in `400 / Invalid Input`, email is used during auth process when token is acquired. – Denis Jan 25 '21 at 18:20
  • Please link to the API client you are using. Google has a few oauth libraries. In any case, you can create a JWT token without an email. See https://developers.google.com/identity/protocols/oauth2/service-account#java – Aerials Jan 26 '21 at 09:05
  • @Aerials I'm using [google-auth-library-oauth2-http](https://github.com/googleapis/google-auth-library-java) along with [google-api-client](https://developers.google.com/api-client-library/java) and [google-api-services-admin-directory](https://developers.google.com/resources/api-libraries/documentation/admin/directory_v1/java/latest/com/google/api/services/admin/directory/package-summary.html). – Denis Jan 26 '21 at 16:03
  • Try following the [explicit credential loading](https://github.com/googleapis/google-auth-library-java#explicit-credential-loading) – Aerials Jan 26 '21 at 16:59

0 Answers0