我的目标是使用从谷歌云automl训练的Firebase云函数导出tflite模型。
我从https://cloud.google.com/vision/automl/object-detection/docs/export-edge并且能够在终端上平滑地卷曲导出模型,但不能在云上使用axios函数。使用以下代码,我得到了401未经授权的错误,甚至我在.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)
您必须以编程方式获取访问令牌。我不明白你是如何用这行代码获得代币的:
Authorization:
"Bearer $(gcloud auth application-default print-access-token)"
您是否试图在Firebase云函数中运行gcloud命令?
在这里,您可以找到有关如何使用OAuth2访问令牌验证您的请求的信息
Google Auth Library Nodejs