java - Google vision API 从文件加载凭证

标签 java credentials google-cloud-vision

我想在创建 Annotator 客户端时使用不同的凭据文件。我目前可以像这样使用 transalte API 来做到这一点:

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

ImageAnnotatorClient 是否有等效的方法?

编辑:我正在使用 google cloud java sdk 版本:1.16.0

最佳答案

凭据 🔗 -> CredentialsProvider 🔗

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);
  • 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 🔗

注意:以上是针对 Java 的。 API for C#是不同的。

关于java - Google vision API 从文件加载凭证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48850479/

相关文章:

java - LWJGL 错误 - 无法找到 GL 函数 glVertexArrayVertexAttribDivisorEXT 的地址

java - 使用 Spring MVC 和 ajax 返回字符串时编码错误

git - 推送到 Git 服务器时 SSL 证书无效

javascript - XHR2 认证请求 - 安全/可伪造?

google-cloud-vision - Google Vision API 返回 PERMISSION_DENIED

javascript - 如何将 Firebase 存储镜像与 Google Cloud Vision API 一起使用?

java - 如何手动将 Spring MVC View 呈现为 html?

Java 验证基本计算器程序中的输入

c# - CredUIPromptForCredentials 强制手动选择用户名

javascript - 为什么 google cloud vision Logo detection 在同一张图片上返回不同的结果?