5

when I try to run firebase functions with cloud vision API and test the functions. I get this error:

ERROR: { Error: 7 PERMISSION_DENIED: Cloud Vision API has not been used in project 563584335869 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=563584335869 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

I do not recognize this project number and I have already enabled the API with the project that I am using. I set the GOOGLE_APPLICATION_CREDENTIALS using the project with the enabled API. What is it that I'm doing wrong?

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36

3 Answers3

6

For those of you who are still having this issue here is what worked for me:

const client = new vision.ImageAnnotatorClient({
            keyFilename: 'serviceAccountKey.json'
          })
James
  • 57
  • 1
  • 1
2

This error message is usually thrown when the application is not being authenticated correctly due to several reasons such as missing files, invalid credential paths, incorrect environment variables assignations, among other causes.

Based on this, I recommend you to validate that the credential file and file path are being correctly assigned, as well as follow the Obtaining and providing service account credentials manually guide in order to explicitly specify your service account file directly into your code; In this way, you will be able to set it permanently and verify if you are passing the service credentials correctly. Additionally, you can take a look on this link that contains a useful step-by-step guide to use Firebase functions with Vision API which includes the Vision object authentication code for Node.js.

Passing the path to the service account key in code example:

// Imports the Google Cloud client library.
const Storage = require('@google-cloud/storage');

// Instantiates a client. Explicitly use service account credentials by
// specifying the private key file. All clients in google-cloud-node have this
// helper, see https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/docs/authentication.md
const storage = new Storage({
  keyFilename: '/path/to/keyfile.json'
});

// Makes an authenticated API request.
storage
  .getBuckets()
  .then((results) => {
    const buckets = results[0];

    console.log('Buckets:');
    buckets.forEach((bucket) => {
      console.log(bucket.name);
    });
  })
  .catch((err) => {
    console.error('ERROR:', err);
  });
Armin_SC
  • 2,130
  • 11
  • 15
  • i have already set export GOOGLE_APPLICATION_CREDENTIALS="[PATH]" and it displays as an environment variable when i type printenv or set in terminal however i still get the same error and cannot use the method in the article as i am useing the newest version of cloud-vision . – Justin Thomas Sep 11 '18 at 15:21
  • You can specify the `keyFilename` parameter within the Vision API client object to point it to the JSON credentials file. This feature is supported by the latest Node.js package version for Vision API, as mentioned in the [Node.js Packages](https://cloud.google.com/nodejs/docs/reference/vision/0.21.x/v1p3beta1.ImageAnnotatorClient#ImageAnnotatorClient) documentation. – Armin_SC Sep 11 '18 at 16:01
  • Additionally, keep in mind that when you set an environment variable value in a session, it is reset every time the session is dropped and it can be accessed only within the session, therefore, it is recommended to explicitly specify your service account file directly into your code. – Armin_SC Sep 11 '18 at 16:01
1

API has not been used in project 563584335869

If you clicked that link has been printed in a console, that guide you to this url. https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=firebase-cli

So that project id means you used the project credential of 'firebase-cli' not yours.

If you tried to set the value of the environment, you can find the variables in your directory

~/.config/firebase/...credentials.json

And sometime it would be not replaced after you tried to override.

But, you can set your credential in code.

You can find the way of getting a credential in here. https://cloud.google.com/iam/docs/creating-managing-service-account-keys

And the credential format is like this one.

{
  "type": "service_account",
  "project_id": 
  "private_key_id": 
  "private_key": 
  "client_email":
  "client_id":
  "auth_uri": 
  "token_uri": 
  "auth_provider_x509_cert_url": 
  "client_x509_cert_url": 
}

I've faced exactly the same error what you have when I used another google API. and resolved that way including the credential inside code.

const textToSpeech = require("@google-cloud/text-to-speech")
const keyfile = require(".././my-project.json")
const config = {
  projectId: keyfile.project_id,
  keyFilename: require.resolve(".././my-project.json")
};
const TTS_Client = new textToSpeech.TextToSpeechClient(config)
Fredric Cliver
  • 175
  • 1
  • 1
  • 9