【问题标题】:Firebase-Function - Scheduled Function keep throwing a error "UNAUTHENTICATED"Firebase-Function - 计划函数不断抛出错误“未验证”
【发布时间】:2021-03-20 15:20:45
【问题描述】:

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

});

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

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

{
  "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"
}

所以我一直在寻找这个 UNAUTHENTICATED 错误,人们说我需要更改一些权限选项。所以我给了 allUsers 和 allAuthenticated Users 一个 Cloud Functions Invoker 权限。还是不行。

对此有任何想法或解决方案吗?谢谢。

【问题讨论】:

  • 我不知道这是否是您的问题的原因,但您应该只在所有异步工作完成后才执行return null;。具体来说,您应该在try() 中的const writeResult = await admin... 之后移动return null;。您还应该在catch() 中的console.log() 之后添加它。
  • 你能正确查看主题和功能吗(云调度器中的日志是否更清楚问题?)firebase.google.com/docs/functions/…
  • @Juancki 我以某种方式通过使用 function.https 实现了相同的结果,并将谷歌调度程序设置为每 5 分钟而不是 pubsub 获取 url。但仍然无法使用 pubsub 的东西.. pubsub 继续发送这个未经身份验证的错误代码。我一直在挖掘它。
  • @RenaudTarnec 我会试试的。该功能在谷歌云测试中运行良好。
  • 它今天自动为我修复了,没有做任何事情

标签: javascript node.js firebase google-cloud-platform google-cloud-functions


【解决方案1】:

您忘记在论点中包含“上下文”。 https://firebase.google.com/docs/functions/schedule-functions

.onRun(async (context) => { ... })

【讨论】:

  • 我对这个答案非常怀疑,因为我没有使用任何参数,而且 Javascript 也没有施加任何参数限制。但它以某种方式为我解决了这个问题?我正在使用打字稿,并从另一个文件中导出方法,例如export async function myFunction(context: EventContext) { }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 2022-12-12
  • 2016-05-01
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多