为 Google.Apis.YouTube.v3 设置代理

本文关键字:设置 代理 v3 YouTube Google Apis | 更新日期: 2023-09-27 17:57:07

>我有以下代码来调用

YouTubeService service = new YouTubeService(new BaseClientService.Initializer()
{
    ApiKey = AppSettings.Variables.YouTube_APIKey,
    ApplicationName = AppSettings.Variables.YouTube_AppName
});
Google.Apis.YouTube.v3.VideosResource.ListRequest request = service.Videos.List("snippet,statistics");
request.Id = string.Join(",", videoIDs);
VideoListResponse response = request.Execute();

这一切都有效,但是当我们将其部署到我们的实时服务器时,它需要通过代理,因此我们将以下内容放入web.config中:

    <defaultProxy useDefaultCredentials="false" enabled="true">
        <proxy usesystemdefault="False" proxyaddress="http://192.111.111.102:8081" />
    </defaultProxy>

但是,这似乎不起作用,因为拨打电话时,我收到以下错误:

System.Net.Sockets.SocketException:无法建立连接,因为目标计算机主动拒绝了它 216.58.213.74:443

有没有办法在代码中手动设置代理?

大致如下:

WebProxy proxy = new WebProxy("192.111.111.102", 8081);
proxy.Credentials = new NetworkCredential(AppSettings.Variables.ProxyUser, AppSettings.Variables.ProxyPassword, AppSettings.Variables.ProxyDomain);
// apply this to the service or request object here

为 Google.Apis.YouTube.v3 设置代理

当然可以!

您可以为 YouTubeService 实例设置System.Net.WebProxy

var _youtubeService = new YouTubeService(yourInitializer);
_youtubeService.HttpClient.MessageHandler.InnerHandler = new HttpClientHandler
{
    Proxy = new WebProxy(your parameters..)
};

注意:如果需要,不要忘记设置其他HttpClientHandler属性(例如AutomaticDecompression

为了解决这个问题,我不得不向 url 发出一个 webrequest 并将结果映射回 VideoListResponse 对象:

try
{
    Uri api = new Uri(string.Format("https://www.googleapis.com/youtube/v3/videos?id={0}&key={1}&part=snippet,statistics", videoIds, AppSettings.Variables.YouTube_APIKey));
    WebRequest request = WebRequest.Create(api);
    WebProxy proxy = new WebProxy(AppSettings.Variables.ProxyAddress, AppSettings.Variables.ProxyPort);
    proxy.Credentials = new NetworkCredential(AppSettings.Variables.ProxyUsername, AppSettings.Variables.ProxyPassword, AppSettings.Variables.ProxyDomain);
    request.Proxy = proxy;
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            return JsonConvert.DeserializeObject<VideoListResponse>(streamReader.ReadToEnd());
        }
    }
}
catch (Exception ex)
{
    ErrorLog.LogError(ex, "Video entity processing error: ");
}
对我来说,

@ABS的解决方案看起来更有效、更"紧凑"。

以下是我如何使用它与 Youtube 频道 API VB.NET:

Dim uriProxy As New Uri("*proxy_url:IP*")
Dim wpyProxy As New WebProxy With {.Address = uriProxy }
Dim get_youtube_channel_channel_work As New youtube_channel
Dim youtube_initialiser As New Google.Apis.Services.BaseClientService.Initializer()
youtube_initialiser.ApiKey = ConfigurationManager.AppSettings("youtube_api_key")
youtube_initialiser.ApplicationName = ConfigurationManager.AppSettings("youtube_api_application_name")
Dim youtube_service As Google.Apis.YouTube.v3.YouTubeService = New YouTubeService(youtube_initialiser)
Dim objChannelListRequest As ChannelsResource.ListRequest = youtube_service.Channels.List("id, snippet, statistics")
youtube_service.HttpClient.MessageHandler.InnerHandler = New HttpClientHandler With {.Proxy = wpyProxy}