【问题标题】:Cannot export tflite model with axios POST method, Error: Request failed with status code 401无法使用 axios POST 方法导出 tflite 模型,错误:请求失败,状态码 401
【发布时间】:2020-07-29 10:21:25
【问题描述】:

我的目标是使用从 google cloud automl 训练的 Firebase 云函数导出 tflite 模型。

我按照https://cloud.google.com/vision/automl/object-detection/docs/export-edge的文档能够在终端上顺利curl导出模型,但不能使用axios on cloud功能。使用以下代码,我得到了 401 Unauthorized Error,即使我在 .env 中设置了 GOOGLE_APPLICATION_CREDENTIALS 并且需要 dotenv 包。

我的问题:是否可以使用 axios POST 请求导出模型?如果那我做错了什么?

//index.js
require("dotenv").config();

//.env
GOOGLE_APPLICATION_CREDENTIALS="./config.json"
async function exportModel() {
  const header = {
    "Content-Type": "application/json;charset=utf-8",
    Authorization:
      "Bearer $(gcloud auth application-default print-access-token)",
  };

  // Construct request
  const request = {
    outputConfig: {
      modelFormat: "tflite",
      gcsDestination: {
        outputUriPrefix: `gs://${output-storage-bucket}/`,
      },
    },
  };

  axios
    .post(
      `https://automl.googleapis.com/v1/projects/${projectId}/locations/us-central1/models/${model_id}:export`,
      request,
      {
        headers: header,
      }
    )
    .then((response) => {
      console.log(response);
      return response;
    })
    .catch((error) => {
      console.log(error);
      return error;
    });
}

错误

Error: Request failed with status code 401
    at createError (/srv/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/srv/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/srv/node_modules/axios/lib/adapters/http.js:236:11)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

【问题讨论】:

    标签: javascript axios google-cloud-functions google-cloud-automl


    【解决方案1】:

    您必须以编程方式获取访问令牌。我不明白您是如何尝试使用这行代码获取令牌的:

     Authorization:
          "Bearer $(gcloud auth application-default print-access-token)" 
    
    

    您是否尝试在 Firebase 云函数中运行 gcloud 命令?

    您可以在此处找到有关如何使用 OAuth2 访问令牌验证您的请求的信息

    Google Auth Library Nodejs

    【讨论】:

    • 我的意思是终端上的 gcloud auth application-default print-access-token 然后在终端上获取结果并替换该代码。哦,你的意思是,我需要一个新颖的身份验证令牌,因为我在 node.js 环境中,对吧?。
    • 如果您使用快捷方式并只是复制粘贴访问令牌,它应该可以工作。但是我仍然不明白如果您只是从控制台获取令牌,为什么要设置GOOGLE_APPLICATION_CREDENTIALS。使用 gcloud 运行命令,测试它是否有效,然后获取令牌并在您的代码中使用它。这不是最合适的方式。
    猜你喜欢
    • 2021-05-18
    • 2018-11-29
    • 1970-01-01
    • 2019-04-11
    • 2020-03-29
    • 2021-06-25
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多