Google Admin Directory API is returning 400 bad request

跟風遠走 提交于 2019-11-28 13:57:29

The problem is that you don't specify the user to use or the domain to use,

you need to add this information here:

Directory directory = new Directory.Builder(TRANSPORT, FACTORY, credential).build();
Directory.Users.List list = directory.users().list();
list.setCustomer("name.surname@yourdomain.com");
Users users = list.execute();

Now if you have permission problem the return code of the error will be 403 = authorization issue.

Jorge Santamaria

The solution is:

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    GoogleCredential credential;

    Collection<String> SCOPES = new ArrayList<String>();
    SCOPES.add("https://www.googleapis.com/auth/admin.directory.user");



    credential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
             .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
             .setServiceAccountUser("adminEmail@yourDomain.com")
             .setServiceAccountScopes(SCOPES)
             .setServiceAccountPrivateKeyFromP12File(
             new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
            .build();

    Directory directory = new Directory.Builder(httpTransport, jsonFactory, credential).build();

    Directory.Users.List list = directory.users().list();
    list.setDomain("yourDomain.com");
    Users users = list.execute();

SERVICE_ACCOUNT_PKCS12_FILE_PATH = File location of the key (API Console) SERVICE_ACCOUNT_EMAIL = Service Account from API COnsole

Add the scope from the control panel of your domain, using the method Service Account https://developers.google.com/drive/delegation

And activate Admin SDK in your API Console.

If you're using Maven, just add the following to your pom.xml:

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-admin</artifactId>
    <version>directory_v1-rev4-1.15.0-rc</version>
</dependency>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!