首页 > 解决方案 > Axios 写入 Firebase 时返回 401 错误

问题描述

我目前有一个计划函数作为云函数运行,它使用 Axios 写入 Firebase 中的实时数据库。但是,我继续收到错误:

'请求失败,状态码 401'

这似乎是一个授权错误。我正在使用 Axios 并使用遗留令牌进行授权,特别是数据库的秘密。

我在这里使用授权错误还是有其他方法可以做到这一点?我遇到的问题是在预定功能中使用授权访问。

应用程序.js

exports.callTo = functions.pubsub.schedule("* * * * *").onRun((context) => {
  let searchTrends;
  googleTrends.dailyTrends({
    trendDate: new Date("2021-10-20"),
    geo: "US",
  }, function(err, res) {
    if (err) {
      functions.logger.error(err);
    } else {
      searchTrends = JSON.parse(res).default.trendingSearchesDays
          .trendingSearches;
      const query = searchTrends[2] + " since:2021-10-20";

      return axios.put("https://testtter-c0e48-default-rtdb.firebaseio.com/searchTrends.json?auth=<account_secret>", {trends: searchTrends})
          .then((res) => {
            functions.logger.log("Result: " + res);
          })
          .catch((error) => {
            functions.logger.log("Error: " + error);
          });
    }
  });
});

标签: node.jsfirebasefirebase-realtime-databaseaxios

解决方案


推荐阅读