Set proxy for Google.Apis.YouTube.v3

那年仲夏 提交于 2019-12-02 16:28:00

问题


I have the following bit of code to make a call to the

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();

This all works but when we deploy it to our live server, it needs to get through a proxy so we put the following into the web.config:

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

However, this doesn't seem to be working as when the call is made, I get the following error:

System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 216.58.213.74:443

Is there a way to manually set the proxy in code?

Something along the lines of:

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

回答1:


To get around this I had to make a webrequest to the url and map the result back to the VideoListResponse object:

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: ");
}


来源:https://stackoverflow.com/questions/38390712/set-proxy-for-google-apis-youtube-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!