Google视觉API从文件加载凭据

Zai*_*mir 3 java credentials google-cloud-vision

创建注释器客户端时,我想使用其他凭证文件。我目前可以使用transalte API做到这一点,如下所示:

 Credentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("path/to/credentials.json"));
 return TranslateOptions.newBuilder().setCredentials(creds).build().getService();  
Run Code Online (Sandbox Code Playgroud)

是否有使用的等效方法ImageAnnotatorClient

编辑:我正在使用谷歌云的Java SDK版本:1.16.0

AnO*_*hew 5

ServiceAccountCredentials-> FixedCredentialsProvider-> ImageAnnotatorSettings.Builder-> ImageAnnotatorSettings->ImageAnnotatorClient

示例(从文档复制):

Credentials myCredentials = ServiceAccountCredentials.fromStream(
    new FileInputStream("path/to/credentials.json"));

ImageAnnotatorSettings imageAnnotatorSettings =
    ImageAnnotatorSettings.newBuilder()
    .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
    .build();

ImageAnnotatorClient imageAnnotatorClient =
    ImageAnnotatorClient.create(imageAnnotatorSettings);
Run Code Online (Sandbox Code Playgroud)

用于此的文档(仍然)令人惊讶地令人沮丧,无法找到和导航。它们之间无法始终保持链接,周围有多个版本,URL更改的频率比他们应有的频繁,并且许多Google自己的链接返回404。

  • ImageAnnotatorClient: com.google.cloud.vision.v1.ImageAnnotatorClient
  • ImageAnnotatorSettings: com.google.cloud.vision.v1.ImageAnnotatorSettings
  • ClientSettings.Builder: com.google.api.gax.rpc.ClientSettings.Builder
  • FixedCredentialsProvider: com.google.api.gax.core.FixedCredentialsProvider
  • ServiceAccountCredentials: com.google.auth.oauth2.ServiceAccountCredentials