使用 youtube API 和 node.js 添加 youtube 评论



我设法从频道获取视频数据,但是当它尝试向视频添加评论时,我失败了。所以在某些时候我可以成功读取数据。

我读过那篇文件:https://developers.google.com/youtube/v3/docs/commentThreads/insert

而且我不确定我是否正确执行了参数。

除了 Node.js 和 Express 之外,我还使用请求-承诺包作为承诺,如果值得一提的话。

const optionsComment = {
method: 'POST',
uri: 'https://www.googleapis.com/youtube/v3/commentThreads',
qs: {
part: 'snippet',
'snippet.channelId': 'a channel id',
'snippet.videoId': 'some video id',
'snippet.topLevelComment.snippet.textOriginal': 'a nice message',
key: "my key"
},
json: true
};
rp(optionsComment)
.then(result=>{
console.log("result of adding comment:", result);
})
.catch(function(err){
console.log("error during add comment");
console.log(err);
});

当我运行代码时,出现此错误:

添加注释时出错

{ 状态代码错误: 401 - {"错误":{"错误":[{"域":"全局","原因":"必需","消息":"需要登录","位置类型":"标头","位置":"授权"}],">

代码":401,"消息":"需要登录"}}
at new StatusCodeError

即使我已登录并尝试评论自己的视频,我也会收到此错误。

也许有人可以给我一个提示。

谢谢!

我和你的问题类似,发送access_tokenqs为我修复它。

'use strict';
let request = require('request');
const sourceId = '< youtube video id>';
const comment_id = 'the comment id';
const comment = 'actual comment';
new Promise((resolve, reject) => {
request({
method: 'POST',
url: 'https://www.googleapis.com/youtube/v3/commentThreads',
headers: {
'User-Agent': 'Request-Promise'
},
body: {
"snippet": {
"videoId": sourceId,
"channelId": comment_id,
"topLevelComment": {
"snippet": {
"textOriginal": comment
}
}
}
},
qs: {
part: 'snippet',
access_token: token
},
json: true
}, function (error, response, body) {
if (error) {
console.log('body', body);
console.log('error in when posting comment ', error.stack);
return reject(error);
}
return resolve(body);
});
});

最新更新