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

时间:2016-07-15 07:51:25

标签: c# google-api youtube-api youtube-data-api

我有以下一些代码来调用

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

3 个答案:

答案 0 :(得分:1)

为了解决这个问题,我不得不对网址进行网络请求,并将结果映射回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: ");
}

答案 1 :(得分:1)

当然可以!

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

var _youtubeService = new YouTubeService(yourInitializer);

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

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

答案 2 :(得分:0)

@ABS 的解决方案在我看来更高效、更“紧凑”。

以下是我在 VB.NET 中通过 Youtube Channel API 使用它的方法:

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}