GoogleApps客户端仅提供SocketTimeOutException

时间:2015-01-08 15:18:14

标签: google-api google-apps google-api-java-client google-admin-sdk google-directory-api

我们编写了一个客户端,使用GoogleNetHttpTransport在googleapps上创建用户,但是当通过superadmin api将用户设置为超级管理员时,我们收到了socketTimeoutException。连接用户自己拥有superAdmin权限。

当我们使用GoogleNetHttpTransport 时,如何增加ReadTimeOut / SocketTimeout。请在下面找到连接到目标和制作客户端的代码片段

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();

1 个答案:

答案 0 :(得分:2)

创建自己的HttpRequestInitializer:

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