Search code examples
google-apigoogle-appsgoogle-api-java-clientgoogle-admin-sdkgoogle-directory-api

GoogleApps client giving SocketTimeOutException only


We wrote a client to create a user on googleapps using the GoogleNetHttpTransport, but we are getting a socketTimeoutException when making the user as Super Admin through superadmin api. Connecting User has superAdmin Privileges itself.

How to increase the ReadTimeOut/SocketTimeout when we are using the GoogleNetHttpTransport. Please find below code snippent on connecting to target and making the client

httpTransport = GoogleNetHttpTransport.newTrustedTransport() ;
GoogleCredential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport)
                    .setJsonFactory(JSON_FACTORY)
                    .setServiceAccountId(serviceAccountId)
                    .setServiceAccountScopes(scopes)
                    .setServiceAccountUser(superAdminUserMail)
                    .setServiceAccountPrivateKeyFromP12File(privateKeyFile).build();

client = new Directory.Builder(httpTransport, JSON_FACTORY,credential)
                      .setApplicationName(APPLICATION_NAME).
                      .setHttpRequestInitializer(credential).build();

//Getting the sockt/timeout exception 
client.users().makeAdmin(userID, admin).execute();

Solution

  • Create your own HttpRequestInitializer:

    new HttpRequestInitializer() {
       @Override
        public void initialize(HttpRequest request) throws IOException {
          credential.initialize(request);
          request.setConnectTimeout(CONNECT_TIMEOUT);
          request.setReadTimeout(READ_TIMEOUT);
        }
     }