我目前正在开发一个应用程序,该应用程序可在调用以下firebase云功能时将帖子发布到Facebook。现在的问题是该请求抛出了错误408:
Error - Post on Facebook { Error: Request failed with status code 408
at createError (/user_code/node_modules/axios/lib/core/createError.js:16:15)
at settle (/user_code/node_modules/axios/lib/core/settle.js:18:12)
at IncomingMessage.handleStreamEnd
(/user_code/node_modules/axios/lib/adapters/http.js:201:11)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:978:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
让我感到困惑的是,通过邮递员的相同请求成功了,没有任何问题:(也许你们当中的一个人知道解决此问题的方法:)
const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });
const admin = require('firebase-admin');
const db = admin.firestore();
const axios = require('axios');
export const facebook = functions.region('europe-
west1').https.onRequest((req, res) => {
return cors(req, res, () => {
// Post data
const data = {
scheduled: null,
message: '',
accessToken: 'token'
}
// Get Post data through ID
db.collection("articles").doc(req.body.id)
.get().then((doc) => {
if (doc.exists) {
data.message = doc.data().meta.facebook.description
data.scheduled = doc.data().meta.facebook.scheduled
} else {
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
// Post on Facebook
if(data.scheduled) {
const message = data.message
const access_token = data.accessToken
axios.post('https://graph.facebook.com/1188810447962053/feed', {
message,
access_token
})
.then((response) => {
res.send(200, 'Success - Posted on Facebook', response)
})
.catch((error) => {
res.send(400, 'Error - Post on Facebook', error);
});
}
})
});
答案 0 :(得分:0)
我的解决方案:
事实证明,发布请求在'request-promise'npm软件包中可以很好地工作,但根本不适用于axios。
这是我现在正在使用的相应代码:
const postToFacebook = {
method: 'POST',
uri: `https://graph.facebook.com/${pageId}/feed`,
qs: {
access_token: access_token,
message: postMessage
}
};
request(postToFacebook)
答案 1 :(得分:0)
您可以在Firebase云功能中使用axios,如下所示:
current_login_at