I am trying to use YouTube API V3 in order to extract the URL of the first video that comes up when you search something on YouTube.
So, I've been reading in the sample codebase and I need the "Search by keyword" and change the value of the maxResults
parameter to 1
.
Since the code is C#, I've tried to use an online converter, but I got one problem. I created a new class, I insert all this code and installed Google API and YouTube API via NuGet.
Edit: This code has been converted from Console App to 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
How should I call this namespace from the Main Form in order to replace my search term searchListRequest.Q = "Google"
with what I entered in the main Form usinga a Textbox?
I'm trying to call it with:
Private Async Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
MsgBox(Await Run())
End Sub
but the MessageBox shows up empty.
Is there a way to call it already changing searchListRequest.Q
?
For example: msgbox(await Run(key word))
.