如何解决,无法加载默认凭据。浏览到 https://cloud.google.com/docs/authentication/getting-started 了解更多信息

How to solve , Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information

我正在 NodeJS 中实现 google automl 来预测图像级别。我已经手动创建了模型、关卡和上传图像。现在我想使用 NodeJS 预测图像的级别。 我写了一个函数,但总是出现以下错误,

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information

代码如下-

async function addfile() {
  console.log("add file called")
  const projectId = "project-name";
  const computeRegion = "us-central1";
  const modelId = "modelid";
  const filePath = "./src/assets/uploads/micro.jpeg";
  const scoreThreshold = "0.9";

  const client = new automl.PredictionServiceClient();
  const modelFullId = client.modelPath(projectId, computeRegion, modelId);
  try {
    const content = fs.readFileSync(filePath, 'base64');
    const params = {};
    if (scoreThreshold) {
      params.score_threshold = scoreThreshold;
    }
    const payload = {};
    payload.image = { imageBytes: content };
    console.log("try block is running")
    var [response] = await client.predict({
      name: modelFullId,
      payload: payload,
      params: params,
      keyFilename: "./src/assets/uploads/service_account_key.json"
    });
    console.log('Prediction results: ' + JSON.stringify(response));
    response.payload.forEach(result => {
      console.log('Predicted class name: ${result.displayName}');
      console.log('Predicted class score: ${result.classification.score}');
    });
  } catch (exception) {
    console.log("exception occur = " + exception);
  }
}

任何解决方案都将不胜感激。

如@Rakesh Saini 所述,当项目中 environment variables are not set or missing.The environment can be set by adding application credentials 并添加其他所需的环境变量(如项目 ID 和位置)时会发生此错误。