2016-08-24 6 views
1

シンプルな機能を備えた.NETベースのYoutube APIクライアントで作業していました。私はこのようなサービスインスタンスを作成する上で行ってきました:YouTube .NET API v3カスタムHttpClient

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のドキュメントは非常に稀です。

ありがとうございます。

+0

Abielita、お返事ありがとうございます。残念なことに、最初のメソッドは古いバージョンのAPIに関連していますが、2つ目のソリューションは、ライブラリを使用せずにAPIで生のHTTP/JSON通信を使用することとはまったく異なりません。私は、このように精巧なフレームワークには単純な解決策があるに違いないと信じています。 – noisefield

答えて

0

これは、related SO questionに、プロキシサーバー経由でAPIを使用する方法を確認しました。このthreadはまた、URLにWebRequestクラスを作成し、バックVideoListResponseオブジェクトに結果をマッピングすることが示唆

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; 

:ここでは、サンプルコードがどのように

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を確認することができます.NETクライアントライブラリでHTTPプロキシを使用します。

関連する問題