2016-11-30 47 views
3

我試圖從代理Web服務器後面運行C#sample twilio application。我的代理服務器需要認證。使用下面的代碼,我可以成功驗證代理服務器並進行Twilio調用。但是,Twilio返回給我一個code 20003(權限被拒絕)。我的AccountSID和AuthToken是正確的。下面的代碼(沒有代理設置)在不需要Web代理服務器的不同環境中工作得很好。通過代理服務器從C#/ .NET調用Twilio API

我的問題是類似的問題和解決方案posted here,使用Java,但我無法複製使用C#/ .NET的Java修復程序。我正在使用.NET SDK 4.5

using System; 
using Twilio; 
using System.Net; 

namespace TestTwilio 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var accountSid = "xx"; 
      var authToken = "yy"; 

       var twilio = new TwilioRestClient(accountSid, authToken);twilio.Proxy = new System.Net.WebProxy("proxy.mycompany.com", 8080); 
        twilio.Proxy.Credentials = new NetworkCredential(「username」, 「password」); 

       var message = twilio.SendMessage("+1xxxxxxxxxx","+1xxxxxxxxxx", "Hello from C#"); 

      if (message.RestException != null) 
      { 
       var error = message.RestException.Message; 
       Console.WriteLine(error); 
       Console.WriteLine(message.RestException.MoreInfo); 
       Console.WriteLine(message.Uri); 
       Console.WriteLine(message.AccountSid); 
       Console.Write("Press any key to continue."); 
       Console.ReadKey(); 
      } 
     } 
    } 
} 

感謝您的幫助。

回答

3

我能夠繞過使用直接API調用的問題。是不是一個優雅的解決方案,原來的問題...所以仍然在尋找正確的方式來做到這一點。以下是有效的代碼。

using System; 
using System.Net; 
using RestSharp; 

namespace TestTwilio 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var client = new RestClient("https://api.twilio.com/2010-04-01/Accounts/{yourTwilioAccountSID}/SMS/Messages.json"); 
      client.Proxy = new System.Net.WebProxy("proxy.mycompany.com", 8080); 
      client.Proxy.Credentials = new NetworkCredential("<<proxyServerUserName>>", "<<proxyServerPassword>>", "<<proxyServerDomain>>"); 
      var request = new RestRequest(Method.POST); 
      request.Credentials = new NetworkCredential("<<your Twilio AccountSID>>", "<<your Twilio AuthToken>>"); 
      request.AddParameter("From", "+1xxxxxxxxxx"); 
      request.AddParameter("To", "+1xxxxxxxxxx"); 
      request.AddParameter("Body", "Testing from C# after authenticating with a Proxy"); 
      IRestResponse response = client.Execute(request); 
      Console.WriteLine(response.Content); 
      Console.ReadKey(); 
     } 
    } 
} 
+0

如果我能更多地讚揚這一點,我會。似乎我的代理服務器正在刪除我的'auth'標頭,將其設置在'Credentials'屬性已解決我的問題! –

相關問題