2011-09-23 3 views
6

Sto provando a sviluppare una semplice applicazione per interagire con Tumblr usando le API v2. Sono in grado di superare ogni fase del flusso di oAuth (grazie alla documentazione trovata su Twitter) e ottenere un token e un segreto autorizzati e autenticati. Ma quando si tratta di pubblicare, ottengo un errore non autorizzato 401. Questo è il relativo frammento di codice:Pubblicare su Tumblr usando oAuth e C#

private void post_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     url = new Uri("http://api.tumblr.com/v2/blog/*myblog*.tumblr.com/post"); 

     Random random = new Random(); 
     nonce = random.Next(1234000, 9999999).ToString(); 

     TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); 
     timestamp = Convert.ToInt64(ts.TotalSeconds).ToString(); 

     string method = "POST"; 

     StringBuilder sb = new StringBuilder(); 
     sb.AppendFormat("{0}&", method); 
     sb.AppendFormat("{0}&", upperCaseUrl(HttpUtility.UrlEncode(url.ToString()))); 
     sb.AppendFormat("body%3D{0}%26", upperCaseUrl(HttpUtility.UrlEncode(textBox.Text))); 
     sb.AppendFormat("oauth_consumer_key%3D{0}%26", consumerKey); 
     sb.AppendFormat("oauth_nonce%3D{0}%26", nonce); 
     sb.AppendFormat("oauth_signature_method%3D{0}%26", "HMAC-SHA1"); 
     sb.AppendFormat("oauth_timestamp%3D{0}%26", timestamp); 
     sb.AppendFormat("oauth_token%3D{0}%26", token); 
     sb.AppendFormat("oauth_version%3D{0}%26", "1.0"); 
     sb.AppendFormat("type%3D{0}", "text"); 

     System.Diagnostics.Debug.WriteLine(sb.ToString()); 

     string signingKey = consumerSecret + '&' + secret; 

     HMACSHA1 signing = new HMACSHA1(Encoding.ASCII.GetBytes(signingKey)); 
     byte[] bytearray = Encoding.ASCII.GetBytes(sb.ToString()); 
     MemoryStream stream = new MemoryStream(bytearray); 
     signature = Convert.ToBase64String(signing.ComputeHash(stream)); 

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 

     StringBuilder header = new StringBuilder(); 
     header.Append("OAuth "); 
     header.AppendFormat("oauth_consumer_key=\"{0}\", ", consumerKey); 
     header.AppendFormat("oauth_token=\"{0}\", ", token); 
     header.AppendFormat("oauth_nonce=\"{0}\", ", nonce); 
     header.AppendFormat("oauth_timestamp=\"{0}\", ", timestamp); 
     header.AppendFormat("oauth_signature_method=\"{0}\", ", "HMAC-SHA1"); 
     header.AppendFormat("oauth_version=\"{0}\", ", "1.0"); 
     header.AppendFormat("oauth_signature=\"{0}\"", upperCaseUrl(HttpUtility.UrlEncode(signature))); 

     System.Diagnostics.Debug.WriteLine(header.ToString()); 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

     request.Method = WebRequestMethods.Http.Post; 

     request.Headers.Add("Authorization", header.ToString()); 

     try 
     { 
      HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
      StreamReader reader = new StreamReader(response.GetResponseStream()); 

      if (reader.ReadToEnd() == "201: Created") 
      { 
       control.Invoke((MethodInvoker)delegate 
       { 
        statusText.Text = "Post successfully sent!"; 
       }); 
      } 
     } 
     catch (Exception ex) 
     { 
      System.Diagnostics.Debug.WriteLine(ex.Message); 
     } 
    } 

La funzione è chiamata cliccando su un pulsante e ottenere il corpo del testo da una casella di testo. La parte di firma è corretta perché è stata testata con altri servizi (Twitter ecc.) E fornisce ancora firme valide per Tumblr. L'intestazione di autorizzazione è praticamente la stessa che ho usato per richiedere un token e autorizzarlo, e la stringa di base contiene solo il tipo di contenuto (testo in questo caso) e il corpo, a parte i parametri standard oauth_ *. Ho anche controllato il codice con gli strumenti disponibili su hueniverse.com, ma ancora nulla.

Probabilmente mi manca qualche intestazione HTTP o sto postando la richiesta sbagliata. Qualche idea? Grazie.

+4

Qualsiasi aggiornamento su come viene confermato esso? Ho un problema simile Continua a recuperare 401 da Tumblr, ma non sei sicuro del perché. –

+0

Avendo lo stesso problema ... Sei riuscito a sistemarlo (e come)? – SimpleVar

risposta

0

c'è un esempio completo su GitHub che possono aiutare a ...

https://github.com/jthigpen/TumblrAPI.NET

+5

Ciò non aiuta perché utilizza nome utente/password per l'autorizzazione, non OAuth. –

+0

ho trovato utile questo [collegamento] (https://www.codeproject.com/Tips/578418/Integrate-Tumblr-into-Csharp-NET-Website). contiene una classe 'Helper' in cui è possibile eseguire facilmente Oauth 1.0a e inoltre è possibile effettuare richieste GET e POST su tumblr per le informazioni dell'utente. Spero che questo aiuti qualcuno –