gpt4 book ai didi

java - 查询 Google 的目录 API - 错误 : 400 Bad Request

转载 作者:行者123 更新时间:2023-11-29 05:37:33 27 4
gpt4 key购买 nike

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

有没有人能解决这个问题?

最佳答案

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

关于java - 查询 Google 的目录 API - 错误 : 400 Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18924521/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com