【问题标题】:Add youtube comments with youtube API and node.js使用 youtube API 和 node.js 添加 youtube 评论
【发布时间】:2018-01-26 13:45:27
【问题描述】:

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

我已阅读该文档:https://developers.google.com/youtube/v3/docs/commentThreads/insert

而且我不确定我是否正确设置了参数。

除了 Node.js 和 Express 之外,如果值得一提,我还在使用 request-promise 包来进行承诺。

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);
  });

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

添加评论时出错

{ StatusCodeError: 401 - {"error":{"errors":[{"domain":"global","re​​ason":"required","message":"需要登录","locationType":" header","location":"Authorization"}],"code":401,"message":"需要登录"}}
在新的 StatusCodeError 处

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

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

谢谢!

【问题讨论】:

  • im logged in 是什么意思?是通过 OAuth2 吗?
  • 哦,是的。我正在使用 passport.js 和 youtube 身份验证。
  • 我认为问题出在“qs”json 元素的某个地方。由于谷歌文档的示例看起来不同,但我不知道如何正确编写它。

标签: javascript node.js express youtube youtube-data-api


【解决方案1】:

我会遇到和你类似的问题,在qs 中发送access_token 为我解决了这个问题。

'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);
  });
});

【讨论】:

  • 但是如何获取 access_token ?
猜你喜欢
  • 2017-06-07
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 2018-10-28
  • 2016-09-11
  • 2018-03-19
  • 2013-01-16
相关资源
最近更新 更多