英文:
Refreshing PubSubHubbub subscription for Youtube API in NodeJS
问题
我成功地通过https://pubsubhubbub.appspot.com/subscribe在Web浏览器上订阅了一个YouTube Feed。我一直在尝试使用NodeJS上的Fetch刷新我的PubSubHubbub订阅,我使用的代码如下:
const details = {
'hub.mode': 'subscribe',
'hub.topic': `https://www.youtube.com/xml/feeds/videos.xml?channel_id=${process.env.ID}`,
'hub.callback': process.env.callback,
'hub.secret': process.env.secret
};
const endpoint = 'http://pubsubhubbub.appspot.com/subscribe';
var formBody = [];
for (const property in details) {
const encodedKey = encodeURIComponent(property);
const encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
const result = await fetch(endpoint, { method: "POST", body: formBody, 'Content-Type': 'application/x-www-form-urlencoded' });
console.log(result.status, await result.text())
期望结果:
状态 204
实际结果:
状态 400,内容:“hub.mode 的值无效”
我期望至少内容会告诉我无效的值是什么,然而最终内容是空白的,我觉得它似乎根本没有成功读取POST的内容。我正在寻找改进我的代码以避免遇到这个问题的方法。
英文:
I managed to subscribe to a Youtube Feed through https://pubsubhubbub.appspot.com/subscribe on a web browser, I've been trying to refresh my subscription to PubSubHubbub through Fetch on NodeJS, the code I used is below
const details = {
'hub.mode': 'subscribe',
'hub.topic': 'https://www.youtube.com/xml/feeds/videos.xml?channel_id=${process.env.ID}',
'hub.callback': process.env.callback,
'hub.secret': process.env.secret
};
const endpoint = 'http://pubsubhubbub.appspot.com/subscribe'
var formBody = [];
for (const property in details) {
const encodedKey = encodeURIComponent(property);
const encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
const result = await fetch(endpoint, { method: "POST", body: formBody, 'Content-Type': 'application/x-www-form-urlencoded'})
console.log(result.status, await result.text())
Expected:
Status 204
Actual:
Status 400, content: "Invalid value for hub.mode:"
I expected at least for the content to tell me what the invalid value was, however it ended up being blank, it appears to me that it did not manage to read the POSTed content at all. I'm looking for ways to improve my code so that I don't encounter this problem.
答案1
得分: 1
我意识到我应该在内容类型标头中指定 charset=UTF-8
。我对发送请求并不是特别精通,所以这对我来说确实是个启发。在添加了这个后,服务器如预期地响应了204,所以我将关闭我的问题。
在试图解决这个问题时,我尝试了一个锻炼,即从child_process模块中导入exec()
函数来运行cURL并发送请求。然而,我建议坚持使用一种语言以使事情更可读,并且使用exec()
直接从shell运行命令可能会产生不希望的效果。
英文:
I realised that I was supposed to specify in the content type header that charset=UTF-8
. I'm not particularly well-versed in sending requests as of yet so this is definitely an eye-opener for me. After adding it in the server responds with 204 as expected and so I'll close my question.
A workout that I tried while trying to fix this was to import the exec()
function from child_process module to run cURL and post the request instead. However I would recommend to stick to one language to make things more readable and using exec()
runs commands directly from the shell which may have undesirable effects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论