java Google Admin Directory API 返回 400 错误请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16729165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:49:09  来源:igfitidea点击:

Google Admin Directory API is returning 400 bad request

javaclientgoogle-directory-api

提问by Rlp

I'm experimenting with the Java client for Google Admin SDK, specifically the Directory API. It's based on the use of GoogleCredentials just like Google Drive API. However, unlike Google Drive I'm getting 400 - bad request errors with Directory. I setup the API as follows:

我正在试验 Google Admin SDK 的 Java 客户端,特别是 Directory API。它基于 GoogleCredentials 的使用,就像 Google Drive API 一样。但是,与 Google Drive 不同的是,我收到了 400 - 目录的错误请求错误。我按如下方式设置 API:

Directory directory = new Directory.Builder(TRANSPORT, FACTORY, credential).build();
Directory.Users.List list = directory.users().list();
Users users = list.execute();

The last execute() produces the error and stack trace listed at the bottom of the post. I'm using the following scopes to authorize myself. I also use the Drive API as you can see.

最后一个 execute() 产生在帖子底部列出的错误和堆栈跟踪。我正在使用以下范围来授权自己。如您所见,我还使用了 Drive API。

https://www.googleapis.com/auth/userinfo.profile
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/admin.directory.user

Accessing the Drive API works like a charm. I can request files without problems. If I perform a simple operation on the Directory API, however, I get the error and stack trace listed below. I should mention that both the Drive API and Directory API refer to the same Google Apps domain for which I'm an administrator. I also enabled both APIs in the Google API console. For the rest, I can't figure out what I'm doing wrong and I'm wondering if anyone else has seen something similar or otherwise has experience with the accessing this API through the Java client.

访问 Drive API 就像一个魅力。我可以毫无问题地请求文件。但是,如果我对 Directory API 执行简单的操作,则会得到下面列出的错误和堆栈跟踪。我应该提到 Drive API 和 Directory API 都指代我作为管理员的同一个 Google Apps 域。我还在 Google API 控制台中启用了这两个 API。其余的,我不知道我做错了什么,我想知道是否有其他人看到过类似的东西,或者有通过 Java 客户端访问这个 API 的经验。

Thanks,

谢谢,

Ralph

拉尔夫

[ERROR] com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 OK
[ERROR] {
[ERROR]   "code" : 400,
[ERROR]   "errors" : [ {
[ERROR]     "domain" : "global",
[ERROR]     "message" : "Bad Request",
[ERROR]     "reason" : "badRequest"
[ERROR]   } ],
[ERROR]   "message" : "Bad Request"
[ERROR] }
[ERROR]     at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
[ERROR]     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:111)
[ERROR]     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:38)
[ERROR]     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.interceptResponse(AbstractGoogleClientRequest.java:312)
[ERROR]     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1042)
[ERROR]     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
[ERROR]     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
[ERROR]     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
[ERROR]     at com.arcus.example.server.api.DirectoryAPI.getEmails(DirectoryAPI.java:29)
[ERROR]     at com.arcus.example.server.action.CreateExampleDelegate.execute(CreateExampleDelegate.java:29)
[ERROR]     at com.arcus.example.server.action.CreateExampleActionHandler.execute(CreateExampleActionHandler.java:28)
[ERROR]     at com.arcus.example.server.action.CreateExampleActionHandler.execute(CreateExampleActionHandler.java:13)
[ERROR]     at com.gwtplatform.dispatch.server.AbstractDispatchImpl.doExecute(AbstractDispatchImpl.java:156)
[ERROR]     at com.gwtplatform.dispatch.server.AbstractDispatchImpl.execute(AbstractDispatchImpl.java:112)
[ERROR]     at com.gwtplatform.dispatch.server.AbstractDispatchServiceImpl.execute(AbstractDispatchServiceImpl.java:84)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[ERROR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]     at java.lang.reflect.Method.invoke(Method.java:601)
[ERROR]     at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
[ERROR]     at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
[ERROR]     at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
[ERROR]     at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
[ERROR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
[ERROR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
[ERROR]     at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
[ERROR]     at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
[ERROR]     at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
[ERROR]     at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
[ERROR]     at com.gwtplatform.dispatch.server.AbstractHttpSessionSecurityCookieFilter.doFilter(AbstractHttpSessionSecurityCookieFilter.java:69)
[ERROR]     at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163)
[ERROR]     at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58)
[ERROR]     at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
[ERROR]     at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[ERROR]     at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[ERROR]     at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[ERROR]     at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[ERROR]     at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[ERROR]     at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[ERROR]     at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[ERROR]     at com.google.appengine.tools.development.DevAppServerServersFilter.doDirectRequest(DevAppServerServersFilter.java:369)
[ERROR]     at com.google.appengine.tools.development.DevAppServerServersFilter.doDirectServerRequest(DevAppServerServersFilter.java:352)
[ERROR]     at com.google.appengine.tools.development.DevAppServerServersFilter.doFilter(DevAppServerServersFilter.java:115)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
[ERROR]     at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
[ERROR]     at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
[ERROR]     at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
[ERROR]     at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
[ERROR]     at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
[ERROR]     at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97)
[ERROR]     at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
[ERROR]     at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:438)
[ERROR]     at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
[ERROR]     at org.mortbay.jetty.Server.handle(Server.java:326)
[ERROR]     at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
[ERROR]     at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
[ERROR]     at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
[ERROR]     at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
[ERROR]     at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
[ERROR]     at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
[ERROR]     at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

回答by Sabrina

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("[email protected]");
Users users = list.execute();

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

现在,如果您有权限问题,错误的返回码将是 403 = 授权问题。

回答by Jorge Santamaria

The solution is:

解决办法是:

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

    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("[email protected]")
             .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

SERVICE_ACCOUNT_PKCS12_FILE_PATH = 密钥的文件位置(API 控制台)SERVICE_ACCOUNT_EMAIL = 来自 API 控制台的服务帐户

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

从域的控制面板添加范围,使用方法服务帐户 https://developers.google.com/drive/delegation

And activate Admin SDK in your API Console.

并在您的 API 控制台中激活 Admin SDK。

回答by Catalin

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

如果您使用的是 Maven,只需将以下内容添加到您的 pom.xml 中:

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-admin</artifactId>
    <version>directory_v1-rev4-1.15.0-rc</version>
</dependency>