获取有关我的频道的数据(YouTube 数据 API v3 + Node.js)

Getting data about my channel (YouTube Data API v3 + Node.js)

根据 YouTube 数据 API documentation,我正在尝试获取有关我的频道的信息。

我编辑了 this example,这里是现在的样子:

const fs = require("fs");
const readline = require("readline");
const { google } = require("googleapis");

const SCOPES = ["https://www.googleapis.com/auth/youtube.readonly"];
const TOKEN_PATH = "token.json";

fs.readFile("credentials.json", (err, content) => {
  if (err) return console.log("Error loading client secret file:", err);
  authorize(JSON.parse(content), listData);
});

function authorize(credentials, callback) {
  const { client_secret, client_id, redirect_uris } = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getNewToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  });
}

function getNewToken(oAuth2Client, callback) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: "offline",
    scope: SCOPES,
  });
  console.log("Authorize this app by visiting this url:", authUrl);
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });
  rl.question("Enter the code from that page here: ", (code) => {
    rl.close();
    oAuth2Client.getToken(code, (err, token) => {
      if (err) return console.error("Error retrieving access token", err);
      oAuth2Client.setCredentials(token);
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
        if (err) return console.error(err);
        console.log("Token stored to", TOKEN_PATH);
      });
      callback(oAuth2Client);
    });
  });
}

function listData(auth) {
  const service = google.youtube({ version: "v3" });
  service.search.list(
    {
      key: <key>,
      auth,
      maxResults: 1,
      part: "snippet",
      mine: true,
    },
    (err, res) => {
      if (err) return console.error("The API returned an error: " + err);
      fs.writeFileSync("current.json", JSON.stringify(res.data));
      console.log(res.data);
    }
  );
}

它没有获取关于我的频道的数据。

这是关于我的频道的有效数据(根据文档):

{
  "kind": "youtube#channelListResponse",
  "etag": "HOiFDW01gFa3OOv6jTbsDBNWk5s",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#channel",
      "etag": "W8UW4c6qU25jUIwK7dbUKZ6myVs",
      "id": "UCdsDGgbcWylt82Xz238WKDw",
      "snippet": {
        "title": "Anton Kryukov",
        "description": "",
        "publishedAt": "2013-12-29T20:20:32Z",
        "thumbnails": {
          "default": { Object },
          "medium": { Object },
          "high": { Object }
        },
        "localized": {
          "title": "Anton Kryukov",
          "description": ""
        }
      }
    }
  ]
}

这是我得到的数据:

{
  "kind": "youtube#searchListResponse",
  "etag": "thdFV9K466l_42uNDJ-UzfurpAM",
  "regionCode": "UA",
  "pageInfo": { "totalResults": 1000000, "resultsPerPage": 1 },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "TDM6iLWG7SU62NtW2bhkoVu9slw",
      "id": { "kind": "youtube#video", "videoId": "0NhViwSjLdk" },
      "snippet": {
        "publishedAt": "2021-02-07T05:57:26Z",
        "channelId": "UCqFzWxSCi39LnW1JKFR3efg",
        "title": "Universal Tram - SNL",
        "description": "A tour guide (Mikey Day) has a hard time controlling his jittery trainee (Dan Levy). Saturday Night Live. Stream now on Peacock: ...",
        "thumbnails": {
          "default": { Object },
          "medium": { Object },
          "high": { Object }
        },
        "channelTitle": "Saturday Night Live",
        "liveBroadcastContent": "none",
        "publishTime": "2021-02-07T05:57:26Z"
      }
    }
  ]
}

你能解释一下我做错了什么吗?

您必须通过您的代码确认:

service.search.list(
    {
      auth,
      maxResults: 1,
      part: "snippet",
      mine: true,
    }
    ...
);

你没有调用 Channels.list API endpoint, but the Search.list 一个。

请注意 Search.list 没有名为 minerequest parameter

代替上面的代码,你必须有类似下面的代码:

service.channels.list(
    {
      auth: auth,
      maxResults: 1,
      part: "snippet",
      mine: true,
    }
    ...
);

请参阅其中的 YouTube 数据 API 的官方文档 Node.js Quickstart for the sample code 以进一步说服自己了解上述修复方法。