Search code examples
google-admin-sdkgoogle-directory-api

Resource not Found userKey Google Directory API using Service Account


Here is my credential built using service account,

GoogleCredential credential = new GoogleCredential.Builder()
             .setJsonFactory(jsonFactory).setTransport(httpTransport)

     .setServiceAccountId("xxxxxxx@developer.gserviceaccount.com")
     .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))

     .setServiceAccountScopes(Collections.singleton(DirectoryScopes.ADMIN_DIRECTORY_USER))

     .build();

And all I am trying is to get a user, where User is com.google.api.services.admin.directory.model.User

User u = null;
try {
    u = service.users().get("test@gmail.com").execute();
} catch (Exception e) {

    e.printStackTrace();
}

    return u.toPrettyString();
}

response returns with code 404 Resource not found?? Is it user need to be created? how to access any gmail user?


Solution

  • Assuming you've already granted your service account domain-wide delegation, you'll need to have it impersonate an admin account using .setServiceAccountUser("admin@domain.com") as shown in the service account documentation. Service accounts are not themselves domain admins, so they need to impersonate one in order to use the Admin SDK APIs.