Answered by:
fetch all videos on a Youtube Channel

Question
-
User944339287 posted
hi guys. I need to fetch all videos playlist from a youtube channel.
this is the url i have refer to https://www.tothenew.com/blog/youtube-apiv3-to-fetch-all-videos-on-a-channel-in-asp-net/#
it's working fine for certain youtube channel (bmw)https://www.youtube.com/bmw but an error occurred when I try this (yskhongdriving) https://www.youtube.com/yskhongdriving
Error message: The remote server returned an error: (404) Not Found.
Public Function ChannelID() As String Dim wc As WebClient = New WebClient With { .Encoding = Encoding.UTF8 } Dim key As String = "XXXXXXXXXXXXXXXXXXXXXXX" Try Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/channels?key=" & key & "&forUsername=" & Me.txt_username.Text & "&part=id") Dim jobj As JObject = CType(JsonConvert.DeserializeObject(jsonstring), JObject) Return jobj("items")(0)("id").ToString() Catch ex As Exception Response.Write(ex.Message & "<br>") End Try 'https://developers.google.com/youtube/v3/docs/channels/list End Function
Public Sub readPlaylist() Dim wc As WebClient = New WebClient With { .Encoding = Encoding.UTF8 } Dim key As String = "XXXXXXXXXXXXXXXXXXXXXXX" Try Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/playlists?part=snippet&key=" & key & "&maxResults=50&channelId=" & ChannelID() & "") Dim jobj As JObject = CType(JsonConvert.DeserializeObject(jsonstring), JObject) For Each entry In jobj("items") DT.Rows.Add(entry("snippet")("title").ToString(), entry("snippet")("description").ToString(), entry("id").ToString(), entry("snippet")("thumbnails")("medium")("url").ToString()) Next Catch ex As Exception Response.Write(ex.Message) End Try End Sub
Protected Sub btn_Search_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Search.Click DT.Columns.Add(New DataColumn("Title", GetType(String))) DT.Columns.Add(New DataColumn("description", GetType(String))) DT.Columns.Add(New DataColumn("videoId", GetType(String))) DT.Columns.Add(New DataColumn("ImageUrl", GetType(String))) readPlaylist() GridView1.DataSource = DT GridView1.DataBind() End Sub
Monday, February 24, 2020 7:48 AM
Answers
-
User-719153870 posted
Hi kengkit,
I built a demo based on the article and your code, and the problem can be rerproduced.
The problem is obvious, this API can not find the yskhongdriving. You might find it wierd since you can literally go to this channel(https://www.youtube.com/yskhongdriving).
Let me explain, according to the API you are using(https://www.googleapis.com/youtube/v3/channels?key={KEY}&forUsername={Username}&part=id), the parameters are below:
According to the API doc, the parameter
forUsername
specifies a YouTube username, thereby requesting the channel associated with that username.Now ,go to the bmw channel(https://www.youtube.com/bmw) and yskhongdriving channel()https://www.youtube.com/bmw), click the HOME to refresh the page:
And you will see the channels' urls are changed and the bmw is https://www.youtube.com/user/BMW/featured while yskhongdriving is https://www.youtube.com/channel/UCXp8tID5WdGufYBe8r9xdLQ/featured.
As you can see, the bmw is a user while yskhongdriving is a channel, that's the reason why the API can not get the response correctly, there's no such user whose name is yskhongdriving.
Let's see the code again:
Try Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/channels?key=" & key & "&forUsername=" & Me.TextBox1.Text & "&part=id") Dim jobj As JObject = CType(JsonConvert.DeserializeObject(jsonstring), JObject) Return jobj("items")(0)("id").ToString() Catch ex As Exception Response.Write(ex.Message & "<br>") End Try
Try Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/playlists?part=snippet&key=" & key & "&maxResults=50&channelId=" & ChannelID() & "") Dim jobj As JObject = CType(JsonConvert.DeserializeObject(jsonstring), JObject) For Each entry In jobj("items") DT.Rows.Add(entry("snippet")("title").ToString(), entry("snippet")("description").ToString(), entry("id").ToString(), entry("snippet")("thumbnails")("medium")("url").ToString()) Next Catch ex As Exception Response.Write(ex.Message) End Try
The point of these code is to get the id by the first API and put the id in the second, while for the yskhongdriving channel, the id is already in the url(https://www.youtube.com/channel/UCXp8tID5WdGufYBe8r9xdLQ/featured), so there's no need to go throug the first API in this case.
In conclusion, i suggest you could add a textbox for those channels like yskhongdriving who already shows there ids in the url and these will go to the second API(
readPlaylist
) directly.Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 25, 2020 3:33 AM