Google vision API load credentials from file
Asked Answered
C

2

5

I want to use a different credentials file when creating my Annotator client. I am currently able to do it with the transalte API like so:

 Credentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("path/to/credentials.json"));
 return TranslateOptions.newBuilder().setCredentials(creds).build().getService();  

Is there an equivalent way of doing it with the ImageAnnotatorClient?

Edit: I am working with google cloud java sdk version: 1.16.0

Carol answered 18/2, 2018 at 10:8 Comment(0)
J
9

Credentials πŸ”— -> CredentialsProvider πŸ”—

ImageAnnotatorSettings.Builder -> ImageAnnotatorSettings -> ImageAnnotatorClient

Example (mostly copied from the docs):

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 πŸ”—

Note: The above is for Java. The API for C# is different.

Jarietta answered 18/2, 2018 at 11:35 Comment(2)
What version is this? I am linking against 1.16.0 and most of the methods you state in your example are not there – Carol
Just noticed the docs you referenced are for version 0.8.3 and it seems they have removed many of the methods and classes since. – Carol
S
-1
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();
        }
Spectator answered 27/12, 2018 at 5:7 Comment(1)
Add some explanation perhaps? – Monjo

© 2022 - 2024 β€” McMap. All rights reserved.