【问题标题】:YouTube .NET API v3 Custom HttpClientYouTube .NET API v3 自定义 HttpClient
【发布时间】:2016-12-31 05:36:17
【问题描述】:

我正在开发一个具有一些简单功能的基于 .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(
                @"domain\user",
                "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 上的文档非常稀缺。

谢谢。

【问题讨论】:

  • Abielita,感谢您的回答。不幸的是,第一种方法与 API 的旧版本有关,而第二种解决方案与使用 API 的原始 HTTP/JSON 通信而无需使用任何库并没有太大区别。我相信,在这样一个复杂的框架中,一定有一些简单的解决方案。

标签: c# proxy youtube-api


【解决方案1】:

我已经检查了related SO question,了解如何通过代理服务器使用 API。下面是示例代码:

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;

这个thread还建议对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: ");
}

您还可以查看此Google documentation,了解如何将 HTTP 代理与 .NET 客户端库一起使用。

【讨论】:

    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2016-11-05
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多