YouTube .NET API v3 Custom HttpClient



我正在开发一个基于。net的Youtube API客户端,带有一些简单的功能。我继续创建一个服务实例,如下所示:

youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    ApiKey = "",
    ApplicationName = "my_wonderful_client"
});

但是,我还需要它能够使用代理来建立连接,所以我继续如下:

if (useProxy)
        {
            Google.Apis.Http.ConfigurableHttpClient customClient = null;
            string proxyUri = "http://proxy.proxy.com";
            NetworkCredential proxyCreds = new NetworkCredential(
                @"domainuser",
                "pass123"
            );
            WebProxy proxy = new WebProxy(proxyUri, 8080)
            {
                UseDefaultCredentials = false,
                Credentials = proxyCreds,
            };

            HttpClientHandler httpClientHandler = new HttpClientHandler()
            {
                Proxy = proxy,
                PreAuthenticate = true,
                UseDefaultCredentials = false,
            };
            Google.Apis.Http.ConfigurableMessageHandler customHandler = new Google.Apis.Http.ConfigurableMessageHandler(httpClientHandler);
            customClient = new Google.Apis.Http.ConfigurableHttpClient(customHandler);
        }

现在我如何使用我的customClient来初始化连接?唉,关于。net API的文档太少了。

谢谢。

我已经检查了有关如何通过代理服务器使用API的相关SO问题。下面是示例代码:

YouTubeRequest request = new YouTubeRequest(settings);
GDataRequestFactory f = (GDataRequestFactory) request.Service.RequestFactory;
IWebProxy iProxy = WebRequest.DefaultWebProxy;
WebProxy myProxy = new WebProxy(iProxy.GetProxy(query.Uri));
// potentially, setup credentials on the proxy here
myProxy.Credentials = CredentialsCache.DefaultCredentials;
myProxy.UseDefaultCredentials = true;
f.Proxy = myProxy;

该线程还建议对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: ");
}

你也可以查看这个关于如何在。net客户端库中使用HTTP代理的谷歌文档

相关内容

  • 没有找到相关文章

最新更新