Google Admin API returns 400 bad requests

I am experimenting with the Java client for the Google Admin SDK, in particular with the directory APIs. It is based on the use of GoogleCredentials as the Google Drive API. However, unlike Google Drive, I get 400 errors with an error in Directory. I configure the API as follows:

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

The last execute () creates the error and stack traces listed at the bottom of the message. I use the following areas to authorize myself. I also use the Drive API, as you can see.

 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. However, if I perform a simple operation in the directory API, I get the error and stack traces listed below. It should be noted that the Drive and Directory APIs are in the same Google Apps domain for which I am the administrator. I also included both APIs in the Google APIs console. For the rest, I canโ€™t understand what Iโ€™m doing wrong, and Iโ€™m wondering if anyone has seen something like this or otherwise has experience accessing this API through the Java client.

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$1.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) 
+4
source share
3 answers

The problem is that you are not specifying the user to use or the domain used,

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 a resolution problem, the error return code will be 403 = authorization problem.

+6
source

Decision:

  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 = key file location (API console) SERVICE_ACCOUNT_EMAIL = service account from API sockets

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

And activate the Admin SDK in the API console.

+5
source

If you are 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> 
+1
source

Source: https://habr.com/ru/post/1243791/


All Articles