在 Java 中更改 Google IAM 的用户代理
Change User-Agent for Google IAM in Java
实例化时,是否可以更改 API 调用 Google IAM 的用户代理信息?我的代码是这样的:
public static Iam initIam() throws IOException, GeneralSecurityException {
InputStream resourceAsStream = GoogleIAM.class.getClassLoader().getResourceAsStream("creds.txt");
GoogleCredential credential = GoogleCredential.fromStream(resourceAsStream);
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
if (credential.createScopedRequired()) {
List<String> scopes = new ArrayList<>();
// Enable full Cloud Platform scope.
scopes.add(IamScopes.CLOUD_PLATFORM);
credential = credential.createScoped(scopes);
}
// Create IAM API object associated with the authenticated transport.
return new Iam.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("test").build();
}
我看到 Google 可以像这样使用 HeaderProvider 进行存储:
Map<String, String> m = ImmutableMap.<String, String>builder().put("user-agent", "my custom useragent").build();
HeaderProvider headerProvider = FixedHeaderProvider.create(m);
但我找不到如何在 Google IAM 中使用 HeaderProvider。
有什么想法吗?
实际上已经在代码中完成了
return new Iam.Builder(httpTransport, JSON_FACTORY,
credential).setApplicationName("test").build();
使用 setApplicationName 方法,您可以在 Stackdriver 日志中看到应用程序名称,即 test,将出现在用户代理字段中
实例化时,是否可以更改 API 调用 Google IAM 的用户代理信息?我的代码是这样的:
public static Iam initIam() throws IOException, GeneralSecurityException {
InputStream resourceAsStream = GoogleIAM.class.getClassLoader().getResourceAsStream("creds.txt");
GoogleCredential credential = GoogleCredential.fromStream(resourceAsStream);
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
if (credential.createScopedRequired()) {
List<String> scopes = new ArrayList<>();
// Enable full Cloud Platform scope.
scopes.add(IamScopes.CLOUD_PLATFORM);
credential = credential.createScoped(scopes);
}
// Create IAM API object associated with the authenticated transport.
return new Iam.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("test").build();
}
我看到 Google 可以像这样使用 HeaderProvider 进行存储:
Map<String, String> m = ImmutableMap.<String, String>builder().put("user-agent", "my custom useragent").build();
HeaderProvider headerProvider = FixedHeaderProvider.create(m);
但我找不到如何在 Google IAM 中使用 HeaderProvider。
有什么想法吗?
实际上已经在代码中完成了
return new Iam.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("test").build();
使用 setApplicationName 方法,您可以在 Stackdriver 日志中看到应用程序名称,即 test,将出现在用户代理字段中