查询Google的目录API - 错误:400错误请求

时间:2013-09-20 19:33:42

标签: java google-admin-sdk

我正在尝试通过Java库使用新的Directory API从谷歌的域中获取用户列表:“google-api-services-admin-directory_v1-rev14-java-1.17.0-rc”。在成功验证并获取令牌(使用管理帐户)后,我尝试执行查询(获取用户列表),但我收到错误400:错误请求。

我正在使用的代码是:

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());
}

接下来,您将找到此问题的日志:

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)

有没有人可以解决这个问题?

1 个答案:

答案 0 :(得分:3)

使用列表时,您需要指定域或客户。 Google Admin Directory API is returning 400 bad request

中的代码示例