我们编写了一个客户端来使用GoogleNetHttpTransport在googleapps上创建用户,但是当通过superadmin api将用户设为超级管理员时,我们得到了一个socketTimeoutException。连接用户本身具有超级管理员权限。
如何在我们使用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();
创建自己的 HttpRequestInitializer:
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
credential.initialize(request);
request.setConnectTimeout(CONNECT_TIMEOUT);
request.setReadTimeout(READ_TIMEOUT);
}
}