小编Soo*_*Cho的帖子

Firebase-Function - 预定函数不断抛出错误“未经授权”

我正在制作一个“预定的”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;

});

Run Code Online (Sandbox Code Playgroud)

这是我的 firebase 函数代码。当我单独运行该功能时它完全正常,并且我还使用“谷歌云平台云功能测试”测试了该功能。当我单独运行一个函数时,数据已成功设置在 firestore 中。

但是,当我部署该功能时它不起作用,我认为这是关于计划功能的东西

{
  "insertId": "184t0npf9hhej7",
  "jsonPayload": {
    "pubsubTopic": "projects/my-project/topics/firebase-schedule-getIndexData-us-central1",
    "targetType": "PUB_SUB",
    "status": "UNAUTHENTICATED",
    "jobName": …
Run Code Online (Sandbox Code Playgroud)

javascript node.js firebase google-cloud-platform google-cloud-functions

6
推荐指数
2
解决办法
374
查看次数