I'm using YouTube API v3 and using the endpoint Search:list I want to get the ID of the first video searched by a title. YouTube gives some code samples and this is one in c# that I'm trying to make it work in vb.net
using System;using System.Collections.Generic;using System.IO;using System.Reflection;using System.Threading;using System.Threading.Tasks;using Google.Apis.Auth.OAuth2;using Google.Apis.Services;using Google.Apis.Upload;using Google.Apis.Util.Store;using Google.Apis.YouTube.v3;using Google.Apis.YouTube.v3.Data;namespace Google.Apis.YouTube.Samples{ /// <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> internal class Search { [STAThread] static void Main(string[] args) { Console.WriteLine("YouTube Data API: Search"); Console.WriteLine("========================"); try { new Search().Run().Wait(); } catch (AggregateException ex) { foreach (var e in ex.InnerExceptions) { Console.WriteLine("Error: " + e.Message); } } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } private async Task Run() { var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "REPLACE_ME", ApplicationName = this.GetType().ToString() }); var searchListRequest = youtubeService.Search.List("snippet"); searchListRequest.Q = "Google"; // Replace with your search term. searchListRequest.MaxResults = 50; // Call the search.list method to retrieve results matching the specified query term. var searchListResponse = await searchListRequest.ExecuteAsync(); List<string> videos = new List<string>(); List<string> channels = new List<string>(); List<string> playlists = new List<string>(); // Add each result to the appropriate list, and then display the lists of // matching videos, channels, and playlists. foreach (var searchResult in searchListResponse.Items) { switch (searchResult.Id.Kind) { case "youtube#video": videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId)); break; case "youtube#channel": channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId)); break; case "youtube#playlist": playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId)); break; } } Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos))); Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels))); Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists))); } }}Using few online converters, I managed to get this vb.net code
Imports SystemImports System.Collections.GenericImports System.IOImports System.ReflectionImports System.ThreadingImports System.Threading.TasksImports Google.Apis.Auth.OAuth2Imports Google.Apis.ServicesImports Google.Apis.UploadImports Google.Apis.Util.StoreImports Google.Apis.YouTube.v3Imports Google.Apis.YouTube.v3.DataPublic Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try New Search.Run().Wait() Catch ex As AggregateException For Each e In ex.InnerExceptions MsgBox("Error: " & e.Message) Next End Try End Sub Private Async Function Run() As Task Dim youtubeService = New YouTubeService(New BaseClientService.Initializer() With { .ApiKey = "REPLACE_ME", .ApplicationName = Me.[GetType]().ToString() }) Dim searchListRequest = youtubeService.Search.List("snippet") searchListRequest.Q = TextBox1.Text searchListRequest.MaxResults = 1 Dim searchListResponse = Await searchListRequest.ExecuteAsync() Dim videos As List(Of String) = New List(Of String)() Dim channels As List(Of String) = New List(Of String)() Dim playlists As List(Of String) = New List(Of String)() For Each searchResult In searchListResponse.Items videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId)) Next MsgBox(String.Format("Videos:" & vbLf & "{0}" & vbLf, String.Join(vbLf, videos))) End FunctionEnd ClassI setted a limit on searchListRequest.MaxResults to return me only 1 result, which means I want to get only the first searched video ID and searchListRequest.Q is connected to a textbox.
In this textbox I'll then write a qwery and I want that once I press the button, a msgbox will appear with the ID of the first searched video..I'm having 3 errors at debug:
- Syntax error on the line New Search.Run().Wait()
- 'Message' is not a member of 'EventArgs'. on the line MsgBox("Error: " & e.Message)
- Value of type 'Exception' cannot be converted to 'EventArgs'. on the line For Each e In ex.InnerExceptions
What Am I supposed to fix them? Thanks
edit:The complete working code is:
Imports SystemImports System.Collections.GenericImports System.IOImports System.ReflectionImports System.ThreadingImports System.Threading.TasksImports Google.Apis.Auth.OAuth2Imports Google.Apis.ServicesImports Google.Apis.UploadImports Google.Apis.Util.StoreImports Google.Apis.YouTube.v3Imports Google.Apis.YouTube.v3.DataPublic Class Form1 Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try Await Run() Catch ex As AggregateException For Each inner In ex.InnerExceptions MsgBox("Error: " & inner.Message) Next End Try End Sub Private Async Function Run() As Task Dim youtubeService = New YouTubeService(New BaseClientService.Initializer() With { .ApiKey = "your api key ", .ApplicationName = Me.[GetType]().ToString() }) Dim searchListRequest = youtubeService.Search.List("snippet") searchListRequest.Q = TextBox1.Text searchListRequest.MaxResults = 1 Dim searchListResponse = Await searchListRequest.ExecuteAsync() Dim videos As List(Of String) = New List(Of String)() Dim channels As List(Of String) = New List(Of String)() Dim playlists As List(Of String) = New List(Of String)() For Each searchResult In searchListResponse.Items videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId)) Next MsgBox(String.Format("Videos:" & vbLf & "{0}" & vbLf, String.Join(vbLf, videos))) End FunctionEnd Class