PermissionError 在 nodejs 的 Firebase 函数中使用 google cloud Vision Api

PermissionError using google cloud Vision Api in Firebase Functions in nodejs

我有一个 firebase 函数,我尝试在我的 firebase 函数中使用 Vision API,但在本地服务时出现此错误

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

首先,我不知道 563584335869 来自哪里,因为当我点击 link 它不是一个有效的项目

其次,我 select 项目并在其上启用了 Vision API,仍然出现相同的错误

代码很直接,反正我加在这里

    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 这是我初始化 firebase admin 的方式 admin.initializeApp(functions.config().firebase);

这里是 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"
  },

更新

这就是 google 云仪表板的样子

更新

我找到了我项目的projectId,和报错信息中的i​​d不一样

更新

我检查了我拥有的所有其他项目的 ID,该编号与任何项目都不匹配

更新

这个问题只发生在我的本地,当部署到 firebase 时它有效

感谢@Chris32,我根据他在评论中的引导找到了解决方案。

由于问题只出现在我的本地服务器上并且在部署时没问题我这样做是为了解决问题 (https://cloud.google.com/vision/docs/before-you-begin)

  1. 导出了一个服务密钥文件
  2. 创建一个名为GOOGLE_APPLICATION_CREDENTIALS
  3. 的环境变量(win 10)
  4. 将服务密钥文件的路径设置为此环境变量
  5. 运行 本地运行

p.s 令人惊讶的是,正如克里斯提到的那样,错误中的项目 ID 是硬编码的,因此具有误导性