1

I'd like to get channel data using the YouTube Data API in a VB.NET application. I can't find any kind of documentation, everything is in .NET and Google documentation is way too cryptic for me. I used to get these data using URL request, but I'd like to do it more... programmatically! I added Google.Apis.YouTube.v3 Nuget, but can't figure how to set credential and retrieve data.

2
  • 1
    I am not aware of any documentation for vb .net but the .net client libary will work with it. Try going though some of these for some ideas stackoverflow.com/questions/tagged/… Commented Dec 18, 2020 at 12:51
  • 1
    If you deem Mike's answer below resolved your issue, then you may consider accepting it and voting it up. Commented Dec 21, 2020 at 17:00

2 Answers 2

3

There is a VB .NET ResumableUpload example in this GITHUB repository that contains some code that should help you get started.


This is some sample code that retrieves all of the videos in the "Uploads" PlayList for the logged on channel.

Imports Google.Apis.YouTube.v3
Imports Google.Apis.YouTube.v3.Data
...
...

            Dim strUploadsListId As String = ""
            Try
                bOK = False
                Dim objChannelListRequest As ChannelsResource.ListRequest = objYouTubeService.Channels.List("contentDetails")
                objChannelListRequest.Mine = True
                Dim objChannelListResponse As ChannelListResponse = objChannelListRequest.Execute
                Dim objChannel As Channel
                For Each objChannel In objChannelListResponse.Items
                    strUploadsListId = objChannel.ContentDetails.RelatedPlaylists.Uploads ' The Uploads PlayList
                    Debug.WriteLine("PlayList ID=" & strUploadsListId)
                Next
                bOK = True
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "ChannelListRequest")
            End Try
            If bOK Then
                Dim objNextPageToken As String = ""
                While Not objNextPageToken Is Nothing
                    Dim objPlayListItemRequest As PlaylistItemsResource.ListRequest = objYouTubeService.PlaylistItems.List("contentDetails")
                    Dim objPlayListItemsListResponse As PlaylistItemListResponse = Nothing
                    objPlayListItemRequest.PlaylistId = strUploadsListId
                    objPlayListItemRequest.MaxResults = 50
                    objPlayListItemRequest.PageToken = objNextPageToken
                    Try
                        bOK = False
                        objPLayListItemsListResponse = objPlayListItemRequest.Execute
                        bOK = True
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical, "PlayListRequest")
                    End Try
                    If bOK Then
                        Dim objPlayListItem As PlaylistItem
                        Dim strVideoIds As New StringBuilder("") With {.Capacity = objPLayListItemsListResponse.Items.Count * 16}
                        For Each objPlayListItem In objPlayListItemsListResponse.Items
                            strVideoIds.Append(objPlayListItem.ContentDetails.VideoId)
                            strVideoIds.Append(",")
                        Next
                        strVideoIds.Remove(strVideoIds.Length - 1, 1) ' Remove Last Character (Extra comma)      
                        Dim objListRequest As VideosResource.ListRequest
                        Dim objVideoListResponse As VideoListResponse = Nothing
                        Try
                            bOK = False
                            objListRequest = New VideosResource.ListRequest(objYouTubeService, "id,snippet,recordingDetails,status,contentDetails") With {.Id = strVideoIds.ToString}
                            Debug.WriteLine("IDs to retrieve: " & strVideoIds.ToString)
                            objVideoListResponse = objListRequest.Execute
                            bOK = True
                        Catch ex As Exception
                            MsgBox(ex.Message, MsgBoxStyle.Critical, "ListRequest")
                        End Try
                        If bOK Then
                            For Each objVideo As Video In objVideoListResponse.Items
                                Dim TheTitle as string = objVideo.Snippet.Title
                                Dim Embeddable as boolean = objVideo.Status.Embeddable
                                Dim dtRecorded as date - Nothing
                                If (Not objVideo.RecordingDetails Is Nothing) AndAlso (Not objVideo.RecordingDetails.RecordingDate Is Nothing) Then
                                    dtRecorded = CDate(objVideo.RecordingDetails.RecordingDate)
                                End If
                                Dim Duration As Date = GetDuration(objVideo.ContentDetails.Duration)
                                Dim Category As string = objVideo.Snippet.CategoryId
                                Dim PrivacyStatus As string = objVideo.Status.PrivacyStatus
                                Dim Description as string = objVideo.Snippet.Description AndAlso
                                '
                                '
                            Next
                        End If
                    End If
                    objNextPageToken = objPlayListItemsListResponse.NextPageToken
                End While
            End If
 '_______________________________________________________


Friend Function GetDuration(ByVal Duration As String) As Date ' Only an elapsed time value
        '  Format returned from YouTube: PT#H#M#S  or PT#M#S or PT#S
        GetDuration = EMPTYDATE
        If Duration IsNot Nothing Then
            If Duration.StartsWith("PT") Then
                Dim x As Integer = 2
                Dim y As Integer = x
                Dim Hours As Integer = 0
                Dim Minutes As Integer = 0
                Dim Seconds As Integer = 0
                Do
                    While y < Duration.Length AndAlso IsNumeric(Duration.Substring(y, 1))
                        y += 1
                    End While
                    If y < Duration.Length Then
                        Select Case Duration.Substring(y, 1)
                            Case "H"
                                Hours = CInt(Duration.Substring(x, y - x))
                            Case "M"
                                Minutes = CInt(Duration.Substring(x, y - x))
                            Case "S"
                                Seconds = CInt(Duration.Substring(x, y - x))
                        End Select
                    End If
                    x = y + 1
                    y = x
                Loop Until x >= Duration.Length
                GetDuration = CDate("01/01/1900 " & Format(Hours, "00") & ":" & Format(Minutes, "00") & ":" & Format(Seconds, "00"))
            End If
        End If
    End Function
Sign up to request clarification or add additional context in comments.

2 Comments

Please note that you may also employ the fields request parameter when invoking the PlaylistItems.list API endpoint, for to obtain from the API only the info you need (a good practice): objPlayListItemRequest.Fields = "items/contentDetails/videoId".
there might be a typo in the link that you provided.
0

I did it, thanks to Mike Meinz and th Visual Studio debugging tools Here the code to get some channels (not necessary yours) data using YouTube Data API in a VB.NET:

        Dim youtube_api_key As String = "Your_Key"
        Dim youtube_api_application_name As String = "Your_Project_Name_In_the_Google_Developper_Console"
        Dim youtube_initialiser As New Google.Apis.Services.BaseClientService.Initializer()
        youtube_initialiser.ApiKey = youtube_api_key
        youtube_initialiser.ApplicationName = youtube_api_application_name

        Dim youtube_service As Google.Apis.YouTube.v3.YouTubeService = New YouTubeService(youtube_initialiser)
        Dim objChannelListRequest As ChannelsResource.ListRequest = youtube_service.Channels.List("id,snippet,statistics")
        objChannelListRequest.Id = youtube_channel
        Dim objChannelListResponse As ChannelListResponse = objChannelListRequest.Execute()
        Debug.Print(objChannelListResponse.Items(0).Snippet.Description)
        Debug.Print(objChannelListResponse.Items(0).Statistics.SubscriberCount) 
        Debug.Print(objChannelListResponse.Items(0).Statistics.VideoCount)
        Debug.Print(objChannelListResponse.Items(0).Statistics.ViewCount)

2 Comments

Please note that Mike's sample code above is quite elaborated, since it implements API result set pagination; thus his code retrieves all the metadata attached to the videos uploaded by any given channel (when replacing objChannelListRequest.Mine = True with objChannelListRequest.id = CHANNEL_ID).
@8oris I want to elaborate and emphasize what @stvar said above about pagination. You need to add pagination using the .NextPageToken property as shown in my example. Without that, you will only retrieve some of the items. I believe five is the default for .MaxResults property. Pagination retrieves .MaxResults number of results at a time until all results are returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.