Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

Any Idea or solution on this? Thank you.

like image 327
Soohan Cho Avatar asked Dec 09 '20 09:12

Soohan Cho


1 Answers

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

.onRun(async (context) => { ... })
like image 53
Timo Looser Avatar answered Nov 01 '22 10:11

Timo Looser