Search code examples
firebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-vision

PermissionError using google cloud Vision Api in Firebase Functions in nodejs


I have a firebase function and I tried to use Vision API in my firebase function, but I get this error when serve locally

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

First of all, I don't know where 563584335869 comes from, since when I click on the link it's not a valid project enter image description here

Second, I select the project and enabled Vision API on it, still getting same error

the code is very straight forward, anyway I add it here

    async analyze(imageStorageLocation: string): Promise<any> {
        const vision = require('@google-cloud/vision');

        const client = new vision.ImageAnnotatorClient();

        const [result] = await client.textDetection(imageStorageLocation);
        const detections = result.textAnnotations;
        console.log('Text:');
        detections.forEach((text: string) => console.log(text));
    }

ange this is how I initialized firebase admin admin.initializeApp(functions.config().firebase);

and here is the package.json

  "dependencies": {
    "@google-cloud/vision": "^1.7.1",
    "@nestjs/common": "^6.0.0",
    "@nestjs/core": "^6.0.0",
    "@nestjs/platform-express": "^6.9.0",
    "express": "^4.17.1",
    "firebase-admin": "^8.7.0",
    "firebase-functions": "^3.3.0",
    "moment-timezone": "^0.5.27",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3"
  },

Update

This is how google cloud dashboard looks like enter image description here

Update

I found projectId of my project, which is different than the id in the error message enter image description here

Update

I checked id of all other projects that I have, the number doesn't match to any

Update

The issue only happens on my local, when deployed to firebase it works


Solution

  • Thanks @Chris32, I found the solution based on his lead in the comments.

    Since the issue was only on my local serve and it was OK on deployment I did this to solve the issue (https://cloud.google.com/vision/docs/before-you-begin)

    1. exported a service key file
    2. create a environment variable (win 10) named GOOGLE_APPLICATION_CREDENTIALS
    3. set the path of service key file to this env variable
    4. run functions locally

    p.s Surprisingly as Chris mentioned the project id in the error is hard coded and so misleading