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

YouTube API: Get upload playlistID for YouTube Channel


I'm trying to figure out the best way to get a channel's upload playlistID given the channel's channelID. E.g. for the channel with channelID

UC9CuvdOVfMPvKCiwdGKL3cQ

The corresponding upload playlistID is

UU9CuvdOVfMPvKCiwdGKL3cQ

Notice the second character has changed from a "C" to a "U"

I can do this transformation via string manipulation, but I'm curious if there's a better, less hacky way to find the upload playlist ID through the official youtube api.

Here's some Kotlin code that shows my issue:

I can find the ChannelID for the "Game Grumps" channel through the following youtube api v3 search:

            val req = youtube.search().list("snippet");
            req.key = {API_KEY}
            req.q = "Game Grumps"
            req.type = "channel"
            val response = req.execute();

The resulting Channel id (response.items[0].snippet.channelId) is UC9CuvdOVfMPvKCiwdGKL3cQ

But when I run the following to try to get the videos uploaded by the channel, I have to use the transformed ChannelID (starting with UU instead of UC)

            val req = youtube.PlaylistItems().list("snippet")
            req.playlistId = "UU9CuvdOVfMPvKCiwdGKL3cQ"
            req.key = {API_KEY}
            val response = req .execute()

If I use the untransformed "UC" channelID, I get the following error: The playlist identified with the requests playlistId parameter cannot be found.

Instead of just replacing the second character with a "U", what's the more robust way (e.g. using the youtube API) of translating a ChannelID to a PlaylistID (for the uploads from that channel)?


Solution

  • I would suggest using the official Youtube API, instead of trying to manipulate the strings. You can follow the instructions here:

    Instructions to get video ids for all uploaded videos for a channel in V3

    1. Get the channel id for the channel you want (you probably only need to do this once, then you can save it)
      • Use search.list
      • Set type to channel
      • Set q to the name of the channel you want
      • Grab the channel id (something like this: "channelId": "UC0X2VuXXXXXXXXXXXXXXXX")
    2. Get the playlist id for the channel uploads using the channel id from step 1 (you probably only need to do this once, then you can save it)
      • Use channels.list
      • Set id to UC0X2VuXXXXXXXXXXXXXXXX from step 1
      • Grab the uploads key from contentDetails (something like this: "uploads": "UU0XXXXXXXXXXXXXXXXXXXX")
    3. Get the videos via the playlistitems in the playlist using the playlist id from step 2
      • Use playlistItems.list
      • Set playlistId to UU0XXXXXXXXXXXXXXXXXXXX from step 2
      • Go through each PlaylistItem and pull out the video id