2013-04-07 13 views
7

Sto provando a pubblicare uno stato di Twitter dalla mia app Web, utilizzando RestSharp. Il seguente codice funziona perfettamente:API RestSharp e Twitter 1.1: Come faccio a urlare correttamente il testo dell'aggiornamento di stato?

var status = "I am fine with posting this status."; 

var client = new RestClient("https://api.twitter.com"); 

// The OAuth keys/tokens/secrets are retrieved elsewhere 
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
    _consumerKey, _consumerSecret, _accessToken, _accessTokenSecret 
); 

var request = new RestRequest("/1.1/statuses/update.json", Method.POST); 
request.AddParameter("status", status, ParameterType.GetOrPost); 

var response = client.Execute(request); 

Tuttavia, questo codice non riesce con un errore di autenticazione se includo uno qualsiasi dei seguenti caratteri nel testo di stato: ! * ' ()

Attraverso un sacco di forum pesca a strascico, ho Abbiamo dedotto che questo è qualcosa a che fare con la codifica della firma OAuth che non corrisponde alla codifica dei parametri POST. Ho trovato this question on SO, ma la ricerca dei problemi di RestSharp su GitHub non rivela nulla di utile.

posso vedere some code in the RestSharp source (UrlEncodeRelaxed) che sembra essere codificante manualmente quel particolare insieme di caratteri per conformarsi alle specifiche di codifica OAuth, quindi ho provato codifica manualmente tali personaggi del mio stato nello stesso modo (con codice preso da RestSharp) prima di passare in, ad esempio:

var status = "I'm NOT fine with posting this status."; 

string[] UriRfc3986CharsToEscape = new[] { "!", "*", "'", "(", ")" }; 
string[] UriRfc3968EscapedHex = new[] { "%21", "%2A", "%27", "%28", "%29" }; 

for (var i = 0; i < UriRfc3986CharsToEscape.Length; i++) 
    status = status.Replace(UriRfc3986CharsToEscape[i], UriRfc3968EscapedHex[i]); 

Ma questo non funziona neanche (ho ancora ottenere l'errore di autenticazione).

Qual è in realtà il problema qui e cosa dovrei fare per codificare correttamente lo stato? O si tratta di un bug RestSharp?

+0

Hai trovato una soluzione o ci hai provato? Sto cercando di affrontare questo ora. –

+0

@Michael_B No, mi dispiace: ho iniziato a esaminarlo, ma non sono arrivato molto lontano prima che altre priorità si intromettessero. –

+0

Ho finito per usare Linq2Twitter per gli aggiornamenti di stato. Funziona con la v1.1 dell'API di Twitter. Non ho mai trovato un modo per risolvere i problemi di OAuth di RestSharp. –

risposta

2

Hai provato a utilizzare la classe HttpUtility integrata in .NET Framework? Puoi trovarlo nello spazio dei nomi System.Web.

string urlEncodedText = HttpUtility.UrlEncode("Your text goes here"); 

MSDN


Questo è sicuramente un problema RestSharp ... Ho suonato in giro con lui per un po 'e il problema è che non v'è comunque di disattivare la codifica standard di cento .. .

void Main() 
{ 
    var ProtectedChars = "0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A".Replace(" 0x", "%").Split(','); 
    var client = new RestClient("https://api.twitter.com"); 

    client.Authenticator = OAuth1Authenticator.ForProtectedResource(
     "_consumerKey", "_consumerSecret", "_accessToken", "_accessTokenSecret" 
    ); 

    var status = "Dogs, Cats & Mice"; 

    string newStatus = string.Empty; 

    foreach (char c in status) 
    { 
     var charBytes = Encoding.UTF8.GetBytes(c.ToString()); 
     var tempText = string.Empty; 

     for (int i = 0; i < charBytes.Count(); i++) 
     { 
      byte b = charBytes[i]; 
      string hex = "%" + b.ToString("X2"); 
      tempText += hex; 
     } 

     if (ProtectedChars.Any(x => x == tempText)) 
     { 
      newStatus += c; 
     } 
     else 
     { 
      newStatus += string.Format("{0:X2}", tempText); 
     } 
    } 

    var request = new RestRequest("/1.1/statuses/update.json", Method.POST); 
    request.AddParameter(new Parameter{ Name = "status", Type = ParameterType.GetOrPost, Value = newStatus }); 

    var response = client.Execute(request); 


} 

In Fiddler ho tenuto sotto controllo le richieste di andare a Twitter ... e questo è quello che ho trovato ...

01.235.
POST https://api.twitter.com/1.1/statuses/update.json HTTP/1.1 
Authorization: /* auth data */ 
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml 
User-Agent: RestSharp/104.4.0.0 
Content-Type: application/x-www-form-urlencoded 
Host: api.twitter.com 
Content-Length: 44 
Accept-Encoding: gzip, deflate 

status=Dogs%252C%2520Cats%2520%2526%2520Mice 

Il problema con la richiesta HTTP, è il corpo della nostra richiesta ... aveva il valore che avevo fornito non è stato toccato ... quindi Twitter avrebbe riconosciuto il messaggio e aggiornato il nostro stato per noi ....

status=Dogs%252C%2520Cats%2520%2526%2520Mice <--- Altered by RestSharp 
status=Dogs%2C%20Cats%20%26%20Mice   <--- What I initially sent 
+0

Il punto è che il * no * metodo di codifica lo fa funzionare: built-in, manuale o altro. Il che mi porta a credere che si tratti di un problema interno di RestSharp. Non ho ancora risolto, però ... –

+0

Ho appena controllato tutto ... e sì .. è un problema di RestSharp ... Ho aggiornato la mia risposta sopra per spiegare cosa sta succedendo ... –

+0

Questo è utile , grazie-ho intenzione di controllare la fonte RestSharp e vedere se riesco a capire una correzione. –

Problemi correlati