簡體   English   中英

Google Vision API Java客戶端:如何在代碼中顯式設置API憑據(不使用環境變量)

[英]Google Vision API java client: how to set API credentials explicitly in code(without using environment variable)

我正在嘗試將我的項目與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();

將此添加到您的客戶,瞧!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM