locked
Display video from youtube channels (with like & view count) RRS feed

  • Question

  • User944339287 posted

    Hi guys.. I'm trying to show all the videos from youtube channels together with the like & view count.
    If can scratch the comments over even better. 

    The following is the URL I referring: 
    https://www.tothenew.com/blog/youtube-apiv3-to-fetch-all-videos-on-a-channel-in-asp-net/

    Having some difficulties when I convert sample code to VB

    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Dim DT As New DataTable
    
        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(System.String)))
            DT.Columns.Add(New DataColumn(description, GetType(System.String)))
            DT.Columns.Add(New DataColumn(videoId, GetType(System.String)))
            DT.Columns.Add(New DataColumn(ImageUrl, GetType(System.String)))
    
            readplaylist()
    
            GridView1.DataSource = DT
            GridView1.DataBind()
    
        End Sub
    
        Public Sub readplaylist()
    
            Dim wc As WebClient = New WebClient() {Encoding = Encoding.UTF8}
            Try
                Dim jsonstring As String = wc.DownloadString(https:= https:, www.googleapis.com/youtube/v3/playlists?part=snippet&key=DeveloperKey&maxResults=50&channelId= + CHanelId() + );, JObject, jobj=(JObjectUnknownJsonConvert.DeserializeObject(jsonstringUnknown)
                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
                Throw
            End Try
    
        End Sub
    
        Public Function ChanelId() As String
            Dim wc As WebClient = New WebClient() {Encoding = Encoding.UTF8}
            Try
                Dim jsonstring As String = wc.DownloadString(https:= https:, www.googleapis.com/youtube/v3/channels?key=DeveloperKey&forUsername= + TextBox1.Text + &part=id);, JObject, jobj=(JObjectUnknownJsonConvert.DeserializeObject(jsonstringUnknown)
                Return jobj(items)(0)(id).ToString
            Catch ex As Exception
                Throw
            End Try
    
        End Function
    
    End Class
    





    Tuesday, January 7, 2020 1:26 AM

Answers

  • User288213138 posted

    Hi kengkit,

    I try the following URL to convert code from c to vb but an error occurred. not able to run.

    I used http://converter.telerik.com/ to convernt the C# to VB, and get below codes(works fine):

    Imports System.Net
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    
    
    Private dt As DataTable = New System.Data.DataTable()
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            dt.Columns.Add(New DataColumn("Title", GetType(String)))
            dt.Columns.Add(New DataColumn("discription", 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
    
        Public Sub readplaylist()
            Dim wc As WebClient = New WebClient With {
                .Encoding = Encoding.UTF8
            }
    
            Try
                Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/playlists?part=snippet&key= Developer Key &maxResults=50&channelId=" & CHanelId() & "")
                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
                Throw
            End Try
        End Sub
    
        Public Function CHanelId() As String
            Dim wc As WebClient = New WebClient With {
                .Encoding = Encoding.UTF8
            }
    
            Try
                Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/channels?key=Developer Key&forUsername=" & TextBox1.Text & "&part=id")
                Dim jobj As JObject = CType(JsonConvert.DeserializeObject(jsonstring), JObject)
                Return jobj("items")(0)("id").ToString()
            Catch ex As Exception
                Throw
            End Try
        End Function

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 8, 2020 7:34 AM

All replies

  • User281315223 posted

    Having some difficulties when I convert sample code to VB

    Can you elaborate on the particular issues you are having? If your issue happens to pertain to anything related to JObjects or the like - you'll need to make sure to add a reference to Json.NET.  I don't think the code itself is going to be too tough of a challenge and I'd imagine you could always just _use C#_ should Visual Basic provide too many issues. 

    My first question would be to ensure that you followed through the appropriate channels and received a Developer API key from YouTube. You aren't going to be able to access any of their content without it. Secondly, it looks like there's a pretty large number of examples from YouTube directly that would probably be the best option for going down this avenue.

    Tuesday, January 7, 2020 4:17 AM
  • User288213138 posted

    Hi kengkit,

    Having some difficulties when I convert sample code to VB

    Can you show me what difficulties you have?

    C # code can be converted into VB code and vice versa. you can use this tool: http://converter.telerik.com/

    Best regards,

    Sam

    Tuesday, January 7, 2020 9:18 AM
  • User944339287 posted

    hi,

    I try the following URL to convert code from c to vb but an error occurred. not able to run.

    https://www.carlosag.net/tools/codetranslator/

    http://converter.telerik.com/

    Tuesday, January 7, 2020 9:48 AM
  • User281315223 posted

    Can you elaborate on which line(s) are problematic from the conversion? The only one that looks like it might be an issue would be the following:

    jobj=(JObjectUnknownJsonConvert.DeserializeObject(jsonstringUnknown)

    If you have a reference to JSON.NET as I mentioned earlier, you should be able to change that to:

    jobj=(JsonConvert.DeserializeObject(jsonstringUnknown)



    Tuesday, January 7, 2020 7:04 PM
  • User288213138 posted

    Hi kengkit,

    I try the following URL to convert code from c to vb but an error occurred. not able to run.

    I used http://converter.telerik.com/ to convernt the C# to VB, and get below codes(works fine):

    Imports System.Net
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    
    
    Private dt As DataTable = New System.Data.DataTable()
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            dt.Columns.Add(New DataColumn("Title", GetType(String)))
            dt.Columns.Add(New DataColumn("discription", 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
    
        Public Sub readplaylist()
            Dim wc As WebClient = New WebClient With {
                .Encoding = Encoding.UTF8
            }
    
            Try
                Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/playlists?part=snippet&key= Developer Key &maxResults=50&channelId=" & CHanelId() & "")
                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
                Throw
            End Try
        End Sub
    
        Public Function CHanelId() As String
            Dim wc As WebClient = New WebClient With {
                .Encoding = Encoding.UTF8
            }
    
            Try
                Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/channels?key=Developer Key&forUsername=" & TextBox1.Text & "&part=id")
                Dim jobj As JObject = CType(JsonConvert.DeserializeObject(jsonstring), JObject)
                Return jobj("items")(0)("id").ToString()
            Catch ex As Exception
                Throw
            End Try
        End Function

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 8, 2020 7:34 AM