c# - 程序未响应 SearchListResponse

标签 c# google-api youtube-api youtube-data-api google-api-dotnet-client

我正在尝试使用 Winforms 应用程序收集 YouTube 视频的数据。当我如下所示调用 YTSearch() 方法时,程序在到达

时停止响应
var slResponse = await slRequest.ExecuteAsync(); 

请求已启动,但并未停止,既未完成,也未触发 try catch 上的 catch。我已经设法使用 Discord.NET 使用 Discord 机器人获得完全相同的功能。在输出中是

Exception thrown: 'Google.GoogleApiException' in Google.Apis.dll
Exception thrown: 'Google.GoogleApiException' in mscorlib.dll
Exception thrown: 'Google.GoogleApiException' in mscorlib.dll`

图书馆:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.YouTube.v3;
using Google.Apis.Services;
using System.Diagnostics;

方法:

public async Task<List<Video>> YTSearch(string query)
{
    string ytAPI = APIKEY; // Ommitted
    var ytService = new YouTubeService(new BaseClientService.Initializer()
    {
        ApiKey = ytAPI,
        ApplicationName = "YouTubeDownloader"
    });
    var slRequest = ytService.Search.List("snipper");
    slRequest.Q = query;
    slRequest.MaxResults = 10;
    var slResponse = await slRequest.ExecuteAsync();
    List<Video> vidList = new List<Video>();
    return vidList;
}

调用:

private void btnSearch_Click(object sender, EventArgs e)
{
    List<Video> vidList = YTSearch(txtSearch.Text).Result;
}

最佳答案

我还没有对此进行测试,但我相当确定您需要使用 ConfigureAwait(false)。因此,在您的 YTSearch 方法中,您需要使用以下行:

var slResponse = await slRequest.ExecuteAsync().ConfigureAwait(false);

这是必需的,因为您要通过使用 .Result 来阻止按钮处理程序中的结果。

This MSDN article有点老了,但是说明了问题。

关于c# - 程序未响应 SearchListResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49709804/

相关文章:

google-app-engine - Google 登录中使用的 Cookie

php - 我应该使用什么范围(google login with hybridauth/oauth)

api - 如何通过通过Google Apps脚本公开的API更新YouTube视频片段描述的HTML?

c# - IObservable TakeLast(n) 和阻塞

c# - winform应用程序主窗体

c# - 当用接口(interface)模糊类时,重写的实现是否仍然存在?

google-api - 来自 Google API 的电影放映时间和 map

video - 如何在我的应用程序中仅流式传输来自 youtube 视频的音频数据?

youtube - YouTube API搜索/列表为空的代码段描述

c# - 实体数据模型不显示 MySql 选项