【问题标题】:Google vision API load credentials from fileGoogle vision API 从文件中加载凭据
【发布时间】:2018-02-18 10:08:11
【问题描述】:

我想在创建 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

【问题讨论】:

    标签: java credentials google-cloud-vision


    【解决方案1】:

    Credentials?->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# 不同。

    【讨论】:

    • 这是什么版本?我正在链接 1.16.0,您在示例中说明的大多数方法都不存在
    • 刚刚注意到您引用的文档是针对 0.8.3 版的,并且似乎他们已经删除了许多方法和类。
    【解决方案2】:
    private static ImageAnnotatorSettings  setImageAnnotator() {
        Credentials myCredentials = null;
        try {
            myCredentials = ServiceAccountCredentials.fromStream(
                    new FileInputStream("C:\\Users\\Pictures\\Cloudvision\\credentials.json"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            ImageAnnotatorSettings imageAnnotatorSettings = null;
            try {
                imageAnnotatorSettings = ImageAnnotatorSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
                .build();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    

    【讨论】:

    • 或许添加一些解释?
    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 2019-10-29
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多