【问题标题】:firebase function returns 403 when doing an http request执行 http 请求时,firebase 函数返回 403
【发布时间】:2020-07-09 14:50:54
【问题描述】:

我开始使用 express 开发这个 node.js 应用程序,通过 http 请求将传感器数据发送到我的数据库(具体来说是从 arduino)。该应用程序的主要目的是从 url 中获取传感器的值,并在我的 cloud firestore 中创建一个新文档。

var admin = require('firebase-admin');
const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
const express = require('express');
var serviceAccount = require("./minicapcollar-firebase-adminsdk-ovdpm-cda3767493.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://minicapcollar.firebaseio.com"
});

let db = admin.firestore();

const app = express();
app.use(cors);

//Declare the field view for the timestamp
let FieldValue = require('firebase-admin').firestore.FieldValue;

app.get('/send/sensorData', (req, res) => {
    var id = 'HpwWiJSGHNbOgJtYi2jM'; //variable to store the id of the collar, TODO: req.query.id
    var lat = req.query.lat/1000000;
    var lon = req.query.lon/1000000;
    var hr = req.query.hr;
    var et = req.query.et;
    var it = req.query.it;
    //Rest of the values

    //Declare the index of the collar id
    let indexRef = db.collection('dogs').doc(id);

    //Declare the index of the position
    let posRef = indexRef.collection('position').doc();

    //Declare the index of the heartrate
    let hrRef = indexRef.collection('heartrate').doc();

    //Declare the index of the external temperature
    let etRef = indexRef.collection('external_temperature').doc();

    //Declare the index of the internal temperature
    let itRef = indexRef.collection('temperature').doc();

    //Save the current time
    var time = FieldValue.serverTimestamp();

    //Set position
    let setPos = posRef.set({
        timestamp: time,
        value: new admin.firestore.GeoPoint(lat,lon)
    }).then(function() {
        console.log("Position data saved to Firestore");
        return null;
    }).catch(function(error) {
        console.log("Got an error: ", error);
    });

    //Set heartrate
    let setHr = hrRef.set({
        timestamp: time,
        value: hr
    }).then(function() {
        console.log("Heartrate data saved to Firestore");
        return null;
    }).catch(function(error) {
        console.log("Got an error: ", error);
    });

    //Set external temperature
    let setET = etRef.set({
        timestamp: time,
        value: et
    }).then(function() {
        console.log("External temperature data saved to Firestore");
        return null;
    }).catch(function(error) {
        console.log("Got an error: ", error);
    });

    //Set internal temperature
    let setIT = itRef.set({
        timestamp: time,
        value: it
    }).then(function() {
        console.log("Data saved to Firestore");
        return null;
    }).catch(function(error) {
        console.log("Got an error: ", error);
    });


    res.send(`All sensor data sent`);


});

app.get('/send/pos', (req, res) => {
    var id = 'HpwWiJSGHNbOgJtYi2jM'; //variable to store the id of the collar, TODO: req.query.id
    var lat = req.query.lat/1000000;
    var lon = req.query.lon/1000000;
    //Rest of the values

    //Declare the index of the collar id
    let indexRef = db.collection('dogs').doc(id);

    //Declare the index of the position
    let posRef = indexRef.collection('position').doc();

    //Save the current time
    var time = FieldValue.serverTimestamp();

    //Set position
    let setPos = posRef.set({
        timestamp: time,
        value: new admin.firestore.GeoPoint(lat,lon)
    }).then(function() {
        console.log("Position data saved to Firestore");
        return null;
    }).catch(function(error) {
        console.log("Got an error: ", error);
    });

    res.send(`Position sent`);
});

app.get('/send/hr', (req, res) => {
    var id = 'HpwWiJSGHNbOgJtYi2jM'; //variable to store the id of the collar, TODO: req.query.id
    var hr = req.query.hr;

    //Declare the index of the collar id
    let indexRef = db.collection('dogs').doc(id);

    //Declare the index of the heartrate
    let hrRef = indexRef.collection('heartrate').doc();

    //Save the current time
    var time = FieldValue.serverTimestamp();

    //Set heartrate
    let setHr = hrRef.set({
        timestamp: time,
        value: hr
    }).then(function() {
        console.log("Heartrate data saved to Firestore");
        return null;
    }).catch(function(error) {
        console.log("Got an error: ", error);
    });

    res.send(setHr & `Heartrate value sent`);
});

app.get('/send/temp', (req, res) => {
    var id = 'HpwWiJSGHNbOgJtYi2jM'; //variable to store the id of the collar, TODO: req.query.id
    var et = req.query.et;
    var it = req.query.it;

    //Declare the index of the collar id
    let indexRef = db.collection('dogs').doc(id);

    //Declare the index of the external temperature
    let etRef = indexRef.collection('external_temperature').doc();

    //Declare the index of the internal temperature
    let itRef = indexRef.collection('temperature').doc();

    //Save the current time
    var time = FieldValue.serverTimestamp();

    //Set external temperature
    let setET = etRef.set({
        timestamp: time,
        value: et
    }).then(function() {
        console.log("External temperature data saved to Firestore");
        return null;
    }).catch(function(error) {
        console.log("Got an error: ", error);
    });

    //Set internal temperature
    let setIT = itRef.set({
        timestamp: time,
        value: it
    }).then(function() {
        console.log("Data saved to Firestore");
        return null;
    }).catch(function(error) {
        console.log("Got an error: ", error);
    });

    res.send(`Temperature values sent`);
});

exports.app = functions.https.onRequest(app);

完成应用程序后,我在本地主机上对其进行了测试,它运行良好。我能够毫无问题地将新数据写入云 Firestore。部署应用程序后,在发送请求时,我收到 403(错误:禁止您的客户端无权从该服务器获取 URL /app/send/pos/?lat=11111111&lon=22222222。)

在 Firestore 规则中,我指定任何人都可以读写以查看是否是问题所在,但问题仍然存在。

我也尝试按照本教程进行操作:https://www.youtube.com/watch?v=LOeioOKUKI8,但我在尝试访问“http://baseURL.firebaseapp.com/timestamp”时遇到了同样的问题,部署时出现 403 错误(在 localhost 上也可以正常工作)

注意:这是我第一次使用 node.js,请原谅任何不好的做法。我是一名电气工程专业的学生,​​所以编程不是我的主要优势。在此先感谢您的帮助!

【问题讨论】:

  • 尝试删除函数并重新部署。还要确保您的 Firebase CLI 完全是最新的。
  • 我已经多次这样做了。我什至部署了示例函数(作为我提供的视频链接中的那个)并且问题仍然存在。关于 firebase CLI,我确保它已更新。我也尝试过使用 google cloud shell,非常感谢您的快速响应!
  • 我建议联系 Firebase 支持以寻求帮助。 support.google.com/firebase/contact/support
  • 谢谢 Dough,我会联系他们,让您知道他们是否能够解决我的问题。
  • @VictorManuel 看看this answer 看看它是否有帮助。昨天有几个人在 Firebase Slack 上讨论它,他们按照这些步骤解决了它。

标签: node.js firebase express google-cloud-firestore google-cloud-functions


【解决方案1】:

谢谢samthecodingman!您提供的answer 成功了!问题出在我的函数的 IAM 配置中。

对于那些不想跳转到另一个链接的人,我在上面的链接中引用Mike Karp提供的答案。

在我看来,Google Cloud Functions 中添加了额外的 IAM 功能,>因此,您可能没有打开 allUser 对该功能的访问权限(仅供参考,这提供了>对整个网络的访问权限)。

  1. 在 Cloud Functions 主页上,突出显示要添加所有 >> 访问权限的 Cloud Functions。
  2. 点击右上角的“显示信息面板”。
  3. 点击“添加成员”并输入“allUsers”,然后在“角色”框中选择>>“Cloud Function”下的“Cloud Function Invokers”。
  4. 点击“保存”

【讨论】:

    【解决方案2】:

    部署函数失败(出于某种奇怪的原因,有时会发生),我猜它没有更新权限。

    重新部署成功,但未更新其权限。

    我不得不删除函数并重新部署以解决此问题。

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 2019-05-12
      • 2020-12-01
      相关资源
      最近更新 更多