question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Configure vision client with API key?

See original GitHub issue

Environment details

  • OS: Mac OS
  • Node.js version: 8.11.1
  • npm version: 5.6.0
  • @google-cloud/vision version: 0.19.0

Steps to reproduce

None, this is a question.

Question

Is it possible to configure the vision client with an API key rather than a Credentials object?

Our use case does not allow us checking in a credentials.json file into the repository, but we are able to use environment variables. It would be nice to be able to use the vision lib rather than POSTing directly to the images:annotate endpoint.

I’ve tried stringifying the Credentials object, then parsing and passing it into the constructor, but then I get strange errors like Auth error:Error: invalid_grant: Invalid JWT Signature. Plus that just seems like more hoops to jump through than necessary, considering the Vision API supports API keys.

// Throws like 10 invalid_grant errors

const visionClient = new vision.ImageAnnotatorClient({
    credentials: JSON.parse(GOOGLE_APPLICATION_CREDENTIALS),
});

visionClient.cropHints(buffer)
    .then(results => console.log(results));

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
alexander-fenstercommented, Jun 23, 2020

@chougron Oh, this issue seems to be forgotten here, sorry for that! Using an API key from the Node.js application is a little bit tricky but totally possible. This gist shows the complete example, here is the main part of it:

const apiKey = 'REDACTED'; // <-- use your API key here
const vision = require('@google-cloud/vision');
const {GoogleAuth, grpc} = require('google-gax');

function getApiKeyCredentials() {
  const sslCreds = grpc.credentials.createSsl();
  const googleAuth = new GoogleAuth();
  const authClient = googleAuth.fromAPIKey(apiKey);
  const credentials = grpc.credentials.combineChannelCredentials(
    sslCreds,
    grpc.credentials.createFromGoogleCredential(authClient)
  );
  return credentials;
}
...
// initialize the client
const sslCreds = getApiKeyCredentials();
const client = new vision.ImageAnnotatorClient({sslCreds});

Anyway, please be advised that we recommend using service account key for authentication on the server side, and OAuth2 for the client side authentication. Please consider using either of those instead of API key authentication.

Let me know if it helps!

1reaction
asrafulcommented, Jul 8, 2020

@alexander-fenster your solution works without any issue .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quickstart: Set up the Vision API - Google Cloud
In the Google Cloud console, click the email address for the service account that you created. · Click Keys. · Click Add key,...
Read more >
How to Get Google Cloud Vision API Key | Daminion
To start working with Auto-tagging in Daminion, you first need to configure Google Cloud Vision API Key. To do this, log into your...
Read more >
Using Google Vision API - Medium
First, create an API key accessing the GCP, going to MENU > APIs & Services > Credentials and clicking on Create credentials >...
Read more >
Using the Vision API with Python - Google Codelabs
How to use Cloud Shell; How to Enable the Google Cloud Vision API; How to Authenticate API requests; How to install the Vision...
Read more >
Using the Google Cloud Vision API with a simple API key
args) throws Exception { // Instantiates a client ImageAnnotatorClient vision = ImageAnnotatorClient.create(); ... BatchAnnotateImagesResponse ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found