Error with GoogleCredentials and Directory in google-admin-sdk

403 views
Skip to first unread message

Gonzalo Zamarro

unread,
Sep 10, 2013, 11:34:32 AM9/10/13
to google-a...@googlegroups.com

Hi!

I'm trying to access google admin-directory  API to create, delete or remove users as administrator.

I'm trying to develop an application that allows to update or delete a user.

I tried to take two paths.

In the first way, I used the code shown: http://stackoverflow.com/questions/16729165/google-admin-directory-api-is-returning-400-bad-request

But adapting the code is like follows:



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

           
GoogleCredential credential;
       
    SCOPES
.add("https://www.googleapis.com/auth/admin.directory.user");
   
GoogleCredential credential;
   
try {
        credential
= new GoogleCredential.Builder()
           
.setTransport(httpTransport)
           
.setJsonFactory(jsonFactory)
           
.setServiceAccountId(
               
"XXXXX...@developer.gserviceaccount.com")
           
.setServiceAccountUser("X...@subdomain.domain.com")//(The administrator account)
           
.setServiceAccountScopes(SCOPES)
           
.setServiceAccountPrivateKeyFromP12File(
               
new File("WEB-INF/KeY.p12")).build();
        credential
.setAccessToken(oauthToken);
   
        resp
.getWriter().println(credential.getServiceAccountId());
           
       
   
Directory directory = new Directory.Builder(httpTransport, jsonFactory, credential).setApplicationName("User Sync Service")
           
.setHttpRequestInitializer(credential).setApplicationName("Example APP").build();
   
    resp
.getWriter().println();
   
Directory.Users.List list = directory.users().list();
    list
.setDomain("subdomain.domain.com");
   
Users users = list.execute();






In this case, the problem is when the Directory object (Directory directory = new Directory.Builder(...) ) instanciate's or executes, and this is the error:



   
Introducir código aquí...

Uncaught exception from servlet
    com
.google.api.client.auth.oauth2.TokenResponseException: 400 OK
   
{
     
"error" : "invalid_grant"
   
}
    at com
.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com
.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:332)
    at com
.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:352)
    at com
.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:269)
    at com
.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:454)
    at com
.google.api.client.auth.oauth2.Credential.intercept(Credential.java:215)
    at com
.google.api.client.http.HttpRequest.execute(HttpRequest.java:854)
    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 com
.ejemploprueba.Inbox.doGet(Inbox.java:85)
    at javax
.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax
.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org
.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org
.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com
.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
    at org
.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com
.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
    at org
.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com
.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
    at org
.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com
.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org
.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org
.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org
.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org
.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org
.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org
.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com
.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266)
    at org
.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org
.mortbay.jetty.Server.handle(Server.java:326)
    at org
.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org
.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at com
.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
    at org
.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com
.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
    at com
.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:439)
    at com
.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:435)
    at com
.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:442)
    at com
.google.tracing.CurrentContext.runInContext(CurrentContext.java:186)
    at com
.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:306)
    at com
.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:298)
    at com
.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:439)
    at com
.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
    at java
.lang.Thread.run(Thread.java:722)




The second way is to create a servlet that call to the api with SCOPE = "https://www.googleapis.com/auth/admin.directory.user", to
 get the acces_token and the user in a second servlet.

In this way I find the problem to create a GoogleCredential and connect to with the services server with the acces_token and the user, and then instantiate the object "Directory" to show in step 1.

Where:

    acces_token:  ya29.AHES6ZREQdCcm7FqZGg3Do0jYxN-XXXXXXXXXXX-YYYYYYYYYYYYYy
    and
    User: us...@subdomain.domain.com

I enabled the "Admin SDK" option into the Google Api Console / Services

How can I fix the error that occurs in the first case?
What is the solution to the second way?
What it's the better solution, the first way or the second?

Thank you very much in advance and greetings.

Vinny P

unread,
Sep 10, 2013, 7:10:57 PM9/10/13
to google-a...@googlegroups.com
On Tue, Sep 10, 2013 at 10:34 AM, Gonzalo Zamarro <gzam...@intelligencepartner.com> wrote:
I'm trying to access google admin-directory  API to create, delete or remove users as administrator.
I'm trying to develop an application that allows to update or delete a user.

How can I fix the error that occurs in the first case?


There are a number of issues that could go wrong here.

First, you may want to double check that the account you're using has the proper authorizations: use the APIs Explorer to test out operations: https://developers.google.com/admin-sdk/directory/v1/reference/users/delete#try-it 

If that works, try using the REST API directly and see if you can authorize properly: https://developers.google.com/admin-sdk/directory/v1/reference/

 
 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com
 

Message has been deleted
Message has been deleted

Gonzalo Zamarro

unread,
Sep 11, 2013, 5:22:19 AM9/11/13
to google-a...@googlegroups.com
Hi Vinny!

First, thank you for the response and speed.

I don't had enabled the Api Acces, and now I can use this API Explorer, and delete the User.

But I don't know how can make this with java and App Engine.

My problem it's with the credential and the Directory instance, like I said in the first entrance.

How Can I connect Correctly to my Direction Api With Java?

Thanks and regards!
Reply all
Reply to author
Forward
0 new messages