【问题标题】:Google Vision API java client: how to set API credentials explicitly in code(without using environment variable)Google Vision API java 客户端:如何在代码中显式设置 API 凭据(不使用环境变量)
【发布时间】:2018-11-11 13:19:41
【问题描述】:

我正在尝试将我的项目与 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,我无法找到方法,并且文档没有说明这方面的任何内容。

【问题讨论】:

  • AFAIK 不可能了

标签: java credentials google-vision explicit client-library


【解决方案1】:

有可能: 创建一个 ImageAnnotatorSettings 像:

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

将此添加到您的客户,瞧!

【讨论】:

    猜你喜欢
    • 2012-07-28
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    相关资源
    最近更新 更多