Youtube API v3 获取上传特定视频的用户的频道 ID

Youtube API v3 get the Channel ID of user that uploaded a certain video

我想获取在 javascript 上上传某个 YouTube 视频的用户的频道 ID,然后比较频道 ID 以查看它是否在数组中。

问题是,我无法确切地找到如何从 javascript 中获取它。我试图得到:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id=[Video ID]&key=[my key]

但它给我每个视频 JSON 解析错误。 任何人都知道如何使用 YouTube API 准确地做到这一点?如果我必须在 html 部分添加外部脚本也没关系。

并且,作为我想要为该视频做的事情的示例:

https://www.youtube.com/watch?v=jNQXAC9IVRw

应该return'UC4QobU6STFB0P71PMvOGN5A'.

知道怎么做吗?

我从 youmightnotneedjquery.com 中提取了 Ajax 代码并对其进行了一些编辑以制作 getJSON 实用函数。这对我有用:

var API_KEY = 'YOUR_API_KEY'; // Replace this with your own key

getJSON(
  'https://www.googleapis.com/youtube/v3/videos?part=snippet&id=jNQXAC9IVRw&key=' + API_KEY,
  function (err, data) {
    if (err) {
        alert(err);
    } else {
        alert(data.items[0].snippet.channelId);
    }
  }
);

function getJSON(url, callback) {
  var request = new XMLHttpRequest();
  request.open('GET', url, true);

  request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
      // We have the JSON, now we try to parse it
      try {
        var data = JSON.parse(request.responseText);
        // It worked, no error (null)
        return callback(null, data);
      } catch(e) {
        // A parsing arror occurred
        console.error(e);
        return callback('An error occurred while parsing the JSON.');
      }
    }
    // If an error occurred while fetching the data
    callback('An error occurred while fetching the JSON.');
  };

  request.onerror = function() {
    // There was a connection error of some sort
    callback('An error occurred while fetching the JSON.');
  };

  request.send();
}