How i fixed "Getting metadata from plugin failed with error: invalid_grant"


cloud GCP vision

I experimeneted with Google Cloud Vision in Node.js and ran this code:

const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();

const fileName = './image.jpg';

client
    .labelDetection(fileName)
    .then(result => {
        result[0].labelAnnotations.forEach(element => {
            console.log(element);
        });
    })
    .catch(err => console.log(err));

In the terminal I got:

{ Error: 400 undefined: Getting metadata from plugin failed with error: invalid_grant
    at ...
  code: '400',
  details:
   'Getting metadata from plugin failed with error: invalid_grant',
  metadata: Metadata { internalRepr: Map {}, options: {} },
  note:
   'Exception occurred in retry method that was not classified as transient' }

Solution

This error means that you don’t have a JSON file with service account key OR your session with the path was expired.

No service account key

If you assume it’s the first issue and you need to get your service account key, please check my post about how I fixed the error about “The incoming JSON object does not contain a client_email field”. This post contains a part about getting a service account key in Google Cloud.

Expired session

For example, you already exported path with:

export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

but you face this issue. You just need to rerun this command again, because it applies to the terminal session. If you close the terminal, PATH will expire.

comments powered by Disqus