在 VB.NET 中使用 Google YouTube 数据 API 获取 YouTube 频道数据
Get YouTube channel data using Google YouTube Data API in VB.NET
我想在 VB.NET 应用程序中使用 YouTube 数据 API 获取频道数据。我找不到任何类型的文档,一切都在 .NET 中,Google 文档对我来说太神秘了。
我曾经使用 URL 请求获取这些数据,但我更想以编程方式进行更多操作!
我添加了 Google.Apis.YouTube.v3 Nuget,但不知道如何设置凭据和检索数据。
this GITHUB repository 中有一个 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
我做到了,感谢 Mike Meinz 和 Visual Studio 调试工具
这里是使用 YouTube 数据 API 在 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)
我想在 VB.NET 应用程序中使用 YouTube 数据 API 获取频道数据。我找不到任何类型的文档,一切都在 .NET 中,Google 文档对我来说太神秘了。 我曾经使用 URL 请求获取这些数据,但我更想以编程方式进行更多操作! 我添加了 Google.Apis.YouTube.v3 Nuget,但不知道如何设置凭据和检索数据。
this GITHUB repository 中有一个 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
我做到了,感谢 Mike Meinz 和 Visual Studio 调试工具 这里是使用 YouTube 数据 API 在 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)