大家好!
我遇到了一些奇怪的问题,不知道该如何解释。我正在使用 Discourse API 来允许某人发帖而无需登录。出于安全原因,这是我将在服务器环境中运行的代码,我选择的环境是基于 Node.js 的 Firebase Cloud Functions。
我的完整代码如下:
const functions = require("firebase-functions");
const fetch = require('node-fetch');
const cors = require("cors")({ origin: true });
exports.makeDiscoursePost = functions.https.onRequest((request, response) => {
cors(request, response, () => {
let myHeaders = new Headers();
myHeaders.append("Api-Key", "<removed>");
myHeaders.append("Api-Username", "system");
myHeaders.append("Content-Type", 'application/json');
let requestOptions = {
method: 'POST',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://forum.kirupa.com/posts.json?title=Testingggg Discourse API, Part 2&raw=Making a brand new post using the API and seeing if things still work.", requestOptions)
.then(r => r.json())
.then(result => response.send(result))
.catch(error => response.send(error));
});
});
我确信这段代码是有效的,因为当我单独运行 fetch 代码时,帖子会被创建。但当它通过 functions 环境运行时,我收到一个“内部服务器错误”。当我查看日志时,我看到“Finished with status: crash”(完成时状态:崩溃)。
有人有什么想法可能是什么问题吗?有人通过 Firebase 的 functions 成功创建过帖子吗?
(抄送 @blake - 我遵循了你使用 HTTP 请求方法的 Node.js 方法,结果是一样的!)
谢谢,
Kirupa