简体   繁体   中英

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

My goal is to use Firebase cloud function export tflite model trained from google cloud automl.

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 . With the following code, I got 401 Unauthorized Error, even i set GOOGLE_APPLICATION_CREDENTIALS in.env and require by dotenv package.

My question: Is it possible to export the model with axios POST request? If then what i did wrong?

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

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)

You have to programmatically get the access token. I do not understand how are you trying to get the token with this line of code:

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

Are you trying to run gcloud commands inside Firebase cloud function?

Here you can find information on how to authenticate your request using OAuth2 access tokens

Google Auth Library Nodejs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM