Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in Youtube API. The following code doesnt work in Form Application. How can I fix it?
C#
public List<string> links = new List<string>();
public List<string> titles = new List<string>();

public MainForm()
{
    InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{
    this.Run().Wait();
    for (int i = 0; i < titles.Count; i++)
    {
        listView1.Items.Add(titles[i]).SubItems.Add(links[i]);
    }
}

private async Task Run()
{
    var youtubeService = new YouTubeService(new BaseClientService.Initializer()
    {
        ApiKey = "myAPIKey",
        ApplicationName = this.GetType().ToString()
    });

    var searchListRequest = youtubeService.Search.List("snippet");
    searchListRequest.Q = "Michael Jackson";
    searchListRequest.MaxResults = 50;
    searchListRequest.VideoCategoryId = "10";
    searchListRequest.Type = "video";

    var searchListResponse = await searchListRequest.ExecuteAsync();
    foreach (var searchResult in searchListResponse.Items)
    {

        switch (searchResult.Id.Kind)
        {
            case "youtube#video":
                titles.Add(searchResult.Snippet.Title);
                links.Add("https://www.youtube.com/watch?v=" + searchResult.Id.VideoId);
                break;
        }
    }
}
Posted
Updated 2-Oct-15 17:55pm
v2
Comments
Krunal Rohit 3-Oct-15 0:05am    
What error is occurring ?

-KR
Amt-Coder 3-Oct-15 6:11am    
There is no error thrown. Just no response to form events like move,click or close after pressed button. I think this problem occurs on the ExecuteAsync code line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900