[英]Firebase-Function - Scheduled Function keep throwing a error “UNAUTHENTICATED”
I am making a "scheduled" firebase-function that gets data from external API source and save it at firestore.我正在制作一个“预定的”firebase 函数,它从外部 API 源获取数据并将其保存在 firestore。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { default: Axios } = require("axios");
admin.initializeApp();
exports.getIndexData = functions.pubsub.schedule('every 5 minutes').onRun(async() => {
try {
const response = await Axios(
"https://financialmodelingprep.com/api/v3/quotes/index?apikey=myAPIKEY"
);
const data = response.data;
const writeResult = await admin
.firestore()
.collection("index")
.doc("sorted-index")
.set({ indexData: data,timeStamp:Date.now()});
} catch (error) {
console.log(error);
}
return null;
});
this is my firebase-function code.这是我的 firebase 功能代码。 and it works totally fine when I run the function separately, and also I tested the function with "google cloud platform cloud function test".
当我单独运行 function 时它工作得很好,而且我用“谷歌云平台云 function 测试”测试了 function。 Data is successfully set it at firestore when I run a function seperately.
当我单独运行 function 时,数据已成功设置在 firestore。
However, it doesn't work when I deploy the function, and I think it is about scheduled-function stuff但是,当我部署 function 时它不起作用,我认为这是关于预定功能的东西
{
"insertId": "184t0npf9hhej7",
"jsonPayload": {
"pubsubTopic": "projects/my-project/topics/firebase-schedule-getIndexData-us-central1",
"targetType": "PUB_SUB",
"status": "UNAUTHENTICATED",
"jobName": "projects/my-project/locations/us-central1/jobs/firebase-schedule-getIndexData-us-central1",
"@type": "type.googleapis.com/google.cloud.scheduler.logging.AttemptFinished"
},
"resource": {
"type": "cloud_scheduler_job",
"labels": {
"job_id": "firebase-schedule-getIndexData-us-central1",
"project_id": "my-project",
"location": "us-central1"
}
},
"timestamp": "2020-12-09T08:48:01.142830977Z",
"severity": "ERROR",
"logName": "projects/my-project/logs/cloudscheduler.googleapis.com%2Fexecutions",
"receiveTimestamp": "2020-12-09T08:48:01.142830977Z"
}
So I was keep searching for this UNAUTHENTICATED error, and people says I hvae to change some permission options.所以我一直在寻找这个 UNAUTHENTICATED 错误,人们说我需要更改一些权限选项。 so I gave allUsers and allAuthenticated Users a Cloud Functions Invoker permission.
所以我给了 allUsers 和 allAuthenticated Users 一个 Cloud Functions Invoker 权限。 still not working.
还是行不通。
Any Idea or solution on this?对此有任何想法或解决方案吗? Thank you.
谢谢你。
You forgot to include "context" in your arguments.您忘记在 arguments 中包含“上下文”。 https://firebase.google.com/docs/functions/schedule-functions
https://firebase.google.com/docs/functions/schedule-functions
.onRun(async (context) => { ... })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.