无法访问调用Firebase http端点 [英] unable to access invoke firebase http end point

查看:72
本文介绍了无法访问调用Firebase http端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写firebase函数,并尝试使其用作HTTP端点.但是,当我尝试访问时,出现错误您的客户端没有从该服务器获取URL/getDetails的权限."

I am writing a firebase function and trying to make it available as an HTTP endpoint. But when I try to access I get the error "Your client does not have permission to get URL /getDetails from this server."

const functions = require('firebase-functions');

const cors = require('cors');

var express = require('express')
var app = express();


'use strict';
app.use(cors);
app.get('/getDetails', function (req, res) {

res.writeHead(200);
var jsonObj = {
    fName: 'Karthik',
    lName: 'Mannepalli'
};
res.end(JSON.stringify(jsonObj));
});

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

我希望输出{"fName":"Karthik","lName":"Mannepalli"},但我得到的是错误:禁止

I expect the output {"fName":"Karthik","lName":"Mannepalli"} but what I get is Error: Forbidden

但是下面的代码给了我正确的输出.在下面的代码中,我没有使用快递

But the following code gives me the right output. In the below code I am not using express

exports.getDetails = functions.https.onRequest(async (req, res) => {
const original = req.query.text;
res.writeHead(200);
var jsonObj = {
    fName: 'Karthik',
    lName: 'Mannepalli'
};
res.end(JSON.stringify(jsonObj));
});

推荐答案

请参见 https: //firebase.google.com/docs/functions/http-events .

    exports.date = functions.https.onRequest((req, res) => {
      // ...
    });

例如,用于调用date()的URL如下所示:

For example, the URL to invoke date() looks like this:

https://us-central1-<project-id>.cloudfunctions.net/date

    const express = require('express');
    const cors = require('cors');
    
    const app = express();
    
    // Automatically allow cross-origin requests
    app.use(cors({ origin: true }));

    // Add middleware to authenticate requests
    app.use(myMiddleware);
    
    // build multiple CRUD interfaces:
    app.get('/:id', (req, res) => res.send(Widgets.getById(req.params.id)));
    app.post('/', (req, res) => res.send(Widgets.create()));
    app.put('/:id', (req, res) => res.send(Widgets.update(req.params.id, req.body)));
    app.delete('/:id', (req, res) => res.send(Widgets.delete(req.params.id)));
    app.get('/', (req, res) => res.send(Widgets.list()));
    
    // Expose Express API as a single Cloud Function:
    exports.widgets = functions.https.onRequest(app);

例如,上面的Express应用示例中用于调用getter的URL如下所示:

For example, the URL to invoke the getter in the Express app example above looks like this:

https://us-central1-<project-id>.cloudfunctions.net/widgets/<id>

因此,对于exports.app = functions.https.onRequest(app);,获取URL为/app/getDetails.不是/getDetails.

So in case of exports.app = functions.https.onRequest(app);, the get URL is /app/getDetails. Not /getDetails.

对于exports.getDetails = functions.https.onRequest(async (req, res),获取的URL为/getDetails.

And in case of exports.getDetails = functions.https.onRequest(async (req, res), the get URL is /getDetails.

这篇关于无法访问调用Firebase http端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆