Search code examples
youtube-apiyoutube-data-apiyoutube-livestreaming-api

I need help understanding determining Youtube username/channel name when you only have the channel ID


So according to https://developers.google.com/youtube/v3/guides/working_with_channel_ids, every account contains a unique channel ID which is independent to what used to be a username. That's all fine and dandy, as channel ID is the way to identify unique accounts. Sure, I'm all for that. Where I am confused, is when I am given a unique channel ID, how can I use this to obtain the channels preferred "username", whether it be some online alias or "real-life" name?

Specifically, I am using this API (https://developers.google.com/youtube/v3/live/docs/liveChatMessages/list?hl=en) to periodically watch as new messages are placed into my broadcast. It works, and provides data which looks like the following:

{
    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]
    }
  }

As you can see, there is an authorChannelId here, which is great... Except for the fact that I can't reasonably read this as a human.

My use case is that I specifically want to have a periodically updating local app which can update me with a console message similarly formatted like so:

Bob Frank: foo bar :D

Even though I technically can have something like the following, it doesn't help me know which of my 5 friends I'm expecting in my live broadcast:

UCLrpeIiuiQuXzarWBeQnswA: foo bar :D

Ah yes... Who are you again? Can you remind me again UCLrpeIiuiQuXzarWBeQnswA?

I have also tried the following API (https://developers.google.com/youtube/v3/docs/channels/list), but it doesn't appear to give the right information either. With part: "snippet" and id: "UCLrpeIiuiQuXzarWBeQnswA" I get the following output:

{
  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'
  }
}

So, any help would be greatly appreciated! Thanks!

Edit: removed access token. Whoops.

Edit2: Answer was in the question, but helped by the accepted answer below. Thank you Benjamin! The real answer is to go further into the final response above.... response.data.items[0].snippet.title. nodejs console.log() in this case did not expand each item and I totally missed this detail. My bad, I'm a novice in node and javascript. You can also use the API key as Benjamin points out, but at this point I've already got access to Oauth2 so I'll keep using the same auth.


Solution

  • Indeed Channels: list is the way to go. Just by accessing and parsing the JSON at this URL https://youtube.googleapis.com/youtube/v3/channels?part=snippet&id=A_CHANNEL_ID&key=YOUR_API_KEY. It has a field title in its snippet which seems to be exactly what you want. Of course you have to change A_CHANNEL_ID with, an authorChannelId, UCLrpeIiuiQuXzarWBeQnswA for instance.