最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase-Function - Scheduled Function keep throwing a error "UNAUTHENTICATED" - Stack Overflow

programmeradmin0浏览0评论

I am making a "scheduled" firebase-function that gets data from external API source and save it at 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(
      ";
    );
    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. and it works totally fine when I run the function separately, and also I tested the function with "google cloud platform cloud function test". Data is successfully set it at firestore when I run a function seperately.

However, it doesn't work when I deploy the function, and I think it is about scheduled-function stuff

{
  "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/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%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. so I gave allUsers and allAuthenticated Users a Cloud Functions Invoker permission. still not working.

Any Idea or solution on this? Thank you.

I am making a "scheduled" firebase-function that gets data from external API source and save it at 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./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. and it works totally fine when I run the function separately, and also I tested the function with "google cloud platform cloud function test". Data is successfully set it at firestore when I run a function seperately.

However, it doesn't work when I deploy the function, and I think it is about scheduled-function stuff

{
  "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./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.%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. so I gave allUsers and allAuthenticated Users a Cloud Functions Invoker permission. still not working.

Any Idea or solution on this? Thank you.

Share Improve this question edited Dec 11, 2020 at 12:11 Juancki 1,8821 gold badge17 silver badges25 bronze badges asked Dec 9, 2020 at 9:00 Soohan ChoSoohan Cho 1238 bronze badges 10
  • 1 I don't know if this is the cause of your problem but you should only do return null; when all the asynchronous work is pleted. Concretely you should move the return null; just after const writeResult = await admin... in the try(). and you should also add it after the console.log() in the catch(). – Renaud Tarnec Commented Dec 9, 2020 at 12:16
  • Can you correctly view the topic and the function as it is indicated (are the logs in the cloud scheduler more clear about the issue?) firebase.google./docs/functions/… – Juancki Commented Dec 10, 2020 at 14:28
  • @Juancki I somehow achieved same result by using function.https thing and set google scheduler to get the url every 5mins instead of pubsub. But still not working with pubsub thing.. pubsub keep sending this UNAUTHENTICATED error code. I am keep digging it. – Soohan Cho Commented Dec 11, 2020 at 0:03
  • @RenaudTarnec I will try. the function worked fine with the test in google cloud. – Soohan Cho Commented Dec 11, 2020 at 0:05
  • 3 It got fixed automatically for me today without doing anything – Alejov Commented Dec 15, 2020 at 3:33
 |  Show 5 more ments

2 Answers 2

Reset to default 5

Try these steps

Disable and re-enable Cloud Scheduler API.

If you are still getting the issue, try this and repeat the above step.

You forgot to include "context" in your arguments. https://firebase.google./docs/functions/schedule-functions

.onRun(async (context) => { ... })
发布评论

评论列表(0)

  1. 暂无评论