java - Google Vision API java客户端: how to set API credentials explicitly in code(without using environment variable)

标签 java credentials google-vision explicit client-library

我正在尝试将我的项目与 Google Vision API 集成

这是 Maven 依赖项,供您检查我尝试集成的客户端版本:

<dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-vision</artifactId>
   <version>1.51.0</version>
</dependency>

文档提供的设置 API 身份验证凭据的方法如下:

Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the 
file path of the JSON file that contains your service account key

我想知道是否有一种方法可以在代码中显式设置凭据,因为这比在我们运行项目的每个环境中设置环境变量更方便。

据我所知,对于以前的客户端版本1.22,可以执行以下操作:

def credentialFile = this.class.classLoader
.getResource(grailsApplication.config.credentialsFile)
GoogleCredential credential = 
GoogleCredential.fromStream(credentialFile.openStream())
        .createScoped(grailsApplication.config.googleScope)

Vision vision = new 
Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), 
jsonFactory, credential).build()

但是对于新的客户端 API,我无法找到方法,并且文档在这方面没有说明任何内容。

最佳答案

有可能: 创建一个 ImageAnnotatorSettings,例如:

ImageAnnotatorSettings ias = ImageAnnotatorSettings.newBuilder()
            .setCredentialsProvider(
                    FixedCredentialsProvider.create(#InputStream of your json key#)
            )
            .build();

将其添加到您的客户端即可!

关于java - Google Vision API java客户端: how to set API credentials explicitly in code(without using environment variable),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249139/

相关文章:

java - GroovyScriptEngine : load groovy scripts from subfolder

windows - Powershell 启动 notepad.exe 并且命令窗口启动并消失

android - android vision如何设置只扫描PDF417?

java - Selenium Grid 示例未正确启动

java - 尝试调试 logback syslog appender 不更新 syslog 的问题

java - Bouncy CaSTLe 问题和 Triple DES

javascript - Access-Control-Expose-Headers 不允许 JS 客户端读取 cookie

vue.js - 单击按钮后如何在 Nuxt 中使用私有(private) API key 发送请求?

android - Google Cloud Vision API 为拖放示例提供了不同的结果

image-processing - Google Cloud Vision API 的 Web 检测功能中缺少描述?