Nodejs云视觉api PERMISSION_DENIED错误项目#

Jus*_*mas 5 node.js google-vision google-cloud-vision

当我尝试使用云视觉 API 运行 firebase 函数并测试函数时。我收到此错误:

错误:{ 错误:7 PERMISSION_DENIED:Cloud Vision API 之前未在项目 563584335869 中使用或已禁用。通过访问 https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=563584335869启用它 ,然后重试。如果您最近启用了此 API,请等待几分钟,以便该操作传播到我们的系统,然后重试。

我无法识别该项目编号,并且我已经为我正在使用的项目启用了 API。我设置了GOOGLE_APPLICATION_CREDENTIALS使用启用了 API 的项目。我做错了什么?

小智 6

对于那些仍然遇到这个问题的人来说,这里对我有用:

const client = new vision.ImageAnnotatorClient({
            keyFilename: 'serviceAccountKey.json'
          })
Run Code Online (Sandbox Code Playgroud)


Arm*_*_SC 2

当由于文件丢失、凭证路径无效、环境变量分配不正确等多种原因导致应用程序未正确进行身份验证时,通常会引发此错误消息。

基于此,我建议您验证凭证文件和文件路径是否已正确分配,并遵循手动获取和提供服务帐户凭证指南,以便直接在代码中显式指定您的服务帐户文件;通过这种方式,您将能够永久设置它并验证您是否正确传递了服务凭据。此外,您可以查看此链接,其中包含将 Firebase 函数与 Vision API 结合使用的有用分步指南,其中包括 Node.js 的 Vision 对象身份验证代码。

在代码示例中传递服务帐户密钥的路径:

// 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);
  });
Run Code Online (Sandbox Code Playgroud)