Discourse APIとFirebase(Cloud Functions)経由での投稿

皆さん、こんにちは!

どうにも不可解な問題に直面しており、解決方法がわかりません。Discourse API を使用して、サインインせずに投稿できるようにしています。セキュリティ上の理由から、これはサーバー環境で実行するコードであり、私が使用している環境は Node.js ベースの Cloud Functions for Firebase です。

私のコード全体は以下のとおりです。

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 環境で実行すると、「Internal Server Error」が発生します。ログを読むと、「Finished with status: crash」と表示されます。

何か問題があると思われる点はありませんか? Firebase の functions を介して投稿作成を機能させたことがある方はいらっしゃいますか?

(cc @blake - HTTP リクエストメソッドを使用した Node.js アプローチに従いましたが、同じ結果になりました!)

よろしくお願いします。
Kirupa

これを解決できたので、他の誰かがこの問題に遭遇した場合に備えて、修正したコードを共有します。

exports.sendfeedback = onRequest({ cors: "https://www.kirupa.com" }, (req, res) => {
  let myHeaders = new fetch.Headers();
  myHeaders.append("Api-Key", "removed");
  myHeaders.append("Api-Username", "kirupaBot");
  myHeaders.append("Content-Type", 'application/json');

  let requestOptions = {
    method: 'POST',
    headers: myHeaders,
    body: JSON.stringify({
      "raw": req.query.raw,
      "title": req.query.title
    })
  };

  fetch("https://forum.kirupa.com/posts.json", requestOptions)
    .then(r => r.json())
    .then(result => res.send(result))
    .catch(error => res.send(error));
});
「いいね!」 1

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.