首页 > 解决方案 > 在 VB.NET 中使用 Google YouTube Data API 获取 YouTube 频道数据

问题描述

我想在 VB.NET 应用程序中使用 YouTube 数据 API 获取频道数据。我找不到任何类型的文档,一切都在 .NET 中,而 Google 文档对我来说太神秘了。我曾经使用 URL 请求获取这些数据,但我想以编程方式进行更多操作!我添加了 Google.Apis.YouTube.v3 Nuget,但不知道如何设置凭据和检索数据。

标签: vb.netyoutube-data-apigoogle-api-dotnet-client

解决方案


这个 GITHUB 存储库中有一个 VB .NET ResumableUpload 示例,其中包含一些可以帮助您入门的代码。


这是一些示例代码,用于检索登录频道的“上传”播放列表中的所有视频。

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

推荐阅读