【发布时间】:2020-10-30 16:56:18
【问题描述】:
我正在尝试使用 YouTube API V3 来提取您在 YouTube 上搜索内容时出现的第一个视频的 URL。
所以,I've been reading in the sample codebase 我需要“按关键字搜索”并将maxResults 参数的值更改为1。
由于代码是 C#,我尝试使用在线转换器,但遇到了一个问题。我创建了一个新类,我插入了所有这些代码并通过 NuGet 安装了 Google API 和 YouTube API。
编辑:此代码已从 Console App 转换为 WinForm:
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Google.Apis.Upload
Imports Google.Apis.Util.Store
Imports Google.Apis.YouTube.v3
Imports Google.Apis.YouTube.v3.Data
Namespace YouTube
''' <summary>
''' YouTube Data API v3 sample: search by keyword.
''' Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
''' See https://developers.google.com/api-client-library/dotnet/get_started
'''
''' Set ApiKey to the API key value from the APIs & auth > Registered apps tab of
''' https://cloud.google.com/console
''' Please ensure that you have enabled the YouTube Data API for your project.
''' </summary>
Public Async Function Run(ByVal videos As String) As Task(Of String)
Dim youtubeService = New YouTubeService(New BaseClientService.Initializer() With {
.ApiKey = "REPLACE_ME",
.ApplicationName = [GetType]().ToString()
})
Dim searchListRequest = youtubeService.Search.List("snippet")
searchListRequest.Q = "Google" ' Replace with your search term.
searchListRequest.MaxResults = 1
' Call the search.list method to retrieve results matching the specified query term.
Dim searchListResponse = Await searchListRequest.ExecuteAsync()
Return searchListResponse.Items.FirstOrDefault()?.Id.VideoId
End Function
End Class
End Namespace
我应该如何从主窗体调用此命名空间,以便将我的搜索词 searchListRequest.Q = "Google" 替换为我使用文本框在主窗体中输入的内容?
我试图用以下方式调用它:
Private Async Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
MsgBox(Await Run())
End Sub
但 MessageBox 显示为空。
有没有办法称它已经改变了searchListRequest.Q?
例如:msgbox(await Run(key word))。
【问题讨论】:
标签: vb.net winforms google-api youtube-api