我正在设置Firebase云函数来对非google服务进行API调用。为了进行测试,我使用jsonplaceholder占位符作为API。我收到的答复是一个403错误。
我已经看过其他问题,相似对此,但答案是升级到一个付费的防火墙帐户将解决问题。升级到火势图后,我修复了Firebase阻塞传出的http调用,但是现在我被任何我尝试过的请求返回的403错误所困扰。
import * as functions from 'firebase-functions';
const cors = require('cors')({origin: true});
export const getJson = functions.https.onRequest(async (req, res) => {
cors( req, res, async ()=> {
try {
let info = await req.get('https://jsonplaceholder.typicode.com/todos/1');
res.send(info);
} catch (error) {
res.status(500).send(`catch block hit: ${error}`);
}
})
})
我期望API调用返回一个json对象:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
我收到的回复是:
"Failed to load resource: the server responded with a status of 403 ()"
谢谢你的帮助。
发布于 2019-08-19 23:52:34
req
是发送到服务器的请求,req.get()
用于获取该请求的头部。如果你想提出请求,你需要自己动手。您可以使用原始节点(例如,https
包)或引入像axios
或isomorphic-fetch
这样的包。就像这样:
const functions = require('firebase-functions')
const axios = require('axios')
const cors = require('cors')({ origin: true });
export const getJson = functions.https.onRequest(async (req, res) => {
cors( req, res, async ()=> {
try {
let info = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
res.send(info.data);
} catch (error) {
res.status(500).send(`catch block hit: ${error}`);
}
})
})
https://stackoverflow.com/questions/57564187
复制相似问题