代码之家  ›  专栏  ›  技术社区  ›  Suren Aznauryan

googlevisionapi java客户端:如何在代码中显式设置API凭证(不使用环境变量)

  •  0
  • Suren Aznauryan  · 技术社区  · 6 年前

    我想把我的项目和 Google Vision API

    以下是maven依赖项,供您检查我尝试与之集成的客户端版本:

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

    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()
    

    1 回复  |  直到 6 年前
        1
  •  2
  •   Sérgio Sousa    6 年前

    有可能: 创建图像注释器设置,如:

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