首页 > 解决方案 > 当您只有频道 ID 时,我需要帮助了解确定 Youtube 用户名/频道名称

问题描述

因此,根据https://developers.google.com/youtube/v3/guides/working_with_channel_ids,每个帐户都包含一个唯一的频道 ID,该 ID 独立于过去的用户名。这一切都很好,因为频道 ID 是识别唯一帐户的方式。当然,我完全赞成。我感到困惑的是,当我获得一个唯一的频道 ID 时,我如何使用它来获取频道首选的“用户名”,无论是一些在线别名还是“现实生活”名称?

具体来说,我正在使用此 API ( https://developers.google.com/youtube/v3/live/docs/liveChatMessages/list?hl=en ) 定期观看新消息被放入我的广播。它可以工作,并提供如下所示的数据:

{
    kind: 'youtube#liveChatMessage',
    etag: 'zPV9PwBFYCiNZH_4PpspXF0ZrSs',
    id: 'LCC.CikqJwoYVUNMcnBlSWl1aVF1WHphcldCZVFuc3dBEgtOaHI4d2NSUHIyQRI5ChpDTC0ybDQzcC12TUNGVVFCZlFvZDVBQUhiQRIbQ09YRjdfM2stdk1DRmZRYmZRb2QzT0FCUUEw',
    snippet: {
      type: 'textMessageEvent',
      liveChatId: 'KicKGFVDTHJwZUlpdWlRdVh6YXJXQmVRbnN3QRILTmhyOHdjUlByMkE',
      authorChannelId: 'UCLrpeIiuiQuXzarWBeQnswA',
      publishedAt: '2021-11-02T23:22:37.070185+00:00',
      hasDisplayContent: true,
      displayMessage: 'testing once again ',
      textMessageDetails: [Object]
    }
  }

如您所见,这里有一个authorChannelId很棒的...除了我无法合理地以人类的身份阅读这一事实。

我的用例是我特别希望有一个定期更新的本地应用程序,它可以使用类似格式的控制台消息更新我:

Bob Frank: foo bar :D

尽管我在技术上可以拥有类似以下的内容,但这并不能帮助我知道我在直播中期待的 5 个朋友中的哪一个:

UCLrpeIiuiQuXzarWBeQnswA: foo bar :D

啊,是的……你又是谁?你能再提醒我一次UCLrpeIiuiQuXzarWBeQnswA吗?

我还尝试了以下 API(https://developers.google.com/youtube/v3/docs/channels/list),但它似乎也没有提供正确的信息。和我part: "snippet"得到id: "UCLrpeIiuiQuXzarWBeQnswA"以下输出:

{
  config: {
    url: 'https://youtube.googleapis.com/youtube/v3/channels?part=snippet&id=UCLrpeIiuiQuXzarWBeQnswA',
    method: 'GET',
    userAgentDirectives: [ [Object] ],
    paramsSerializer: [Function (anonymous)],
    headers: {
      'x-goog-api-client': 'gdcl/5.0.5 gl-node/17.0.1 auth/7.10.1',
      'Accept-Encoding': 'gzip',
      'User-Agent': 'google-api-nodejs-client/5.0.5 (gzip)',
      Authorization: 'Bearer',
      Accept: 'application/json'
    },
    params: { part: 'snippet', id: 'UCLrpeIiuiQuXzarWBeQnswA' },
    validateStatus: [Function (anonymous)],
    retry: true,
    responseType: 'json'
  },
  data: {
    kind: 'youtube#channelListResponse',
    etag: 'wjX6CX2hdUPFAc4y6OCUDDjXT6o',
    pageInfo: { totalResults: 1, resultsPerPage: 5 },
    items: [ [Object] ]
  },
  headers: {
    'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
    'cache-control': 'private',
    connection: 'close',
    'content-encoding': 'gzip',
    'content-type': 'application/json; charset=UTF-8',
    date: 'Wed, 03 Nov 2021 01:30:32 GMT',
    server: 'scaffolding on HTTPServer2',
    'transfer-encoding': 'chunked',
    vary: 'Origin, X-Origin, Referer',
    'x-content-type-options': 'nosniff',
    'x-frame-options': 'SAMEORIGIN',
    'x-xss-protection': '0'
  },
  status: 200,
  statusText: 'OK',
  request: {
    responseURL: 'https://youtube.googleapis.com/youtube/v3/channels?part=snippet&id=UCLrpeIiuiQuXzarWBeQnswA'
  }
}

因此,任何帮助将不胜感激!谢谢!

编辑:删除访问令牌。哎呀。

Edit2:答案在问题中,但下面接受的答案有所帮助。谢谢本杰明!真正的答案是进一步了解上面的最终响应...... response.data.items[0].snippet.title。在这种情况下,nodejs console.log() 没有展开每个项目,我完全错过了这个细节。我的错,我是节点和 javascript 的新手。您也可以使用 Benjamin 指出的 API 密钥,但此时我已经可以访问 Oauth2,所以我将继续使用相同的身份验证。

标签: youtube-apiyoutube-data-apiyoutube-livestreaming-api

解决方案


确实频道:列表是要走的路。只需在此 URL https://youtube.googleapis.com/youtube/v3/channels?part=snippet&id=A_CHANNEL_ID&key=YOUR_API_KEY访问和解析 JSON 。它的“片段”中有一个字段“标题”,这似乎正是您想要的。当然,您必须A_CHANNEL_ID使用 authorChannelId 进行更改,UCLrpeIiuiQuXzarWBeQnswA例如。


推荐阅读