kamspawn
kamspawn

Reputation: 21

Querying Google’s Directory API - Error: 400 Bad Request

I’m trying to get the list of users from a google’s domain using the new Directory API through the Java library: “google-api-services-admin-directory_v1-rev14-java-1.17.0-rc”. After a successful authentication and getting the token (with an administrative account) I tried to execute the query (obtaining the users list), but I get the Error 400: Bad request.

The code I’m using is this:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
    .setAccessType("offline").setApprovalPrompt("force").setDataStoreFactory(oData).addRefreshListener(credentialRefreshListener).build();
String url = flow.newAuthorizationUrl().setRedirectUri(redirectURL).build();
System.out.println(url);
System.out.println("Enter Responsse Url");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
AuthorizationCodeResponseUrl authResponse = new AuthorizationCodeResponseUrl(br.readLine());

if (authResponse.getError() != null) {
    System.out.println("authorization denied...");// authorization denied...
} else {
    GoogleTokenResponse response = flow.newTokenRequest(authResponse.getCode()).setRedirectUri(redirectURL).execute();
    System.out.println("Access token: " + response.getAccessToken() + " Expire: " +
            response.getExpiresInSeconds());
    Credential credential = flow.createAndStoreCredential(response,"user");
    Credential credential1 = flow.loadCredential("user");
    Directory directory = new Directory.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("TestAPP/1.0").build();
    Directory.Users.List list = directory.users().list();
    Users users = list.execute();
    System.out.println(users.size());
}

Next, you’ll find the log of this issue:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Bad Request",
    "reason" : "badRequest"
  } ],
  "message" : "Bad Request"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at net.easysol.google.CreateUser.main(CreateUser.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Does anyone a way to fix this problem?   

Upvotes: 2

Views: 5549

Answers (1)

Silvano
Silvano

Reputation: 1005

When using list, you need to specify either domain or customer. Code example in Google Admin Directory API is returning 400 bad request

Upvotes: 4

Related Questions