簡體   English   中英

無法使用 axios POST 方法導出 tflite model,錯誤:請求失敗,狀態碼 401

[英]Cannot export tflite model with axios POST method, Error: Request failed with status code 401

我的目標是使用從谷歌雲 automl 訓練的 Firebase cloud function export tflite model。

I followed the document from https://cloud.google.com/vision/automl/object-detection/docs/export-edge and be able to curl export the model smoothly on terminal, but not with axios on cloud function . 使用以下代碼,我得到了 401 Unauthorized Error,即使我在.env 中設置了 GOOGLE_APPLICATION_CREDENTIALS 並要求 dotenv package。

我的問題:是否可以使用 axios POST 請求導出 model? 如果那我做錯了什么?

//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)

您必須以編程方式獲取訪問令牌。 我不明白您如何嘗試使用這行代碼獲取令牌:

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

您是否嘗試在 Firebase 雲 function 中運行 gcloud 命令?

在這里,您可以找到有關如何使用 OAuth2 訪問令牌驗證您的請求的信息

谷歌身份驗證庫 Nodejs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM