2011-09-23 103 views
6

我正在尝试开发一个简单的应用程序来与使用v2 API的Tumblr进行交互。我能够通过oAuth流程中的每一步(感谢Twitter上的文档)并获得经过授权和验证的令牌和秘密。但是当发布时,我会收到401未经授权的错误。这是相关的代码片段:使用oAuth和C发布在Tumblr上#

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); 
     } 
    } 

该功能在单击按钮时调用并从文本框中获取正文文本。签名部分是正确的,因为它已经与其他服务(Twitter等)进行过测试,并且它仍然为Tumblr本身提供了有效的签名。授权标头与我用来请求标识并授权它的基本相同,除了oauth_ *标准参数外,基本字符串仅包含内容的类型(在这种情况下为文本)和正文。我还用hueniverse.com上提供的工具检查了代码,但仍然没有任何结果。

也许我错过了一些HTTP头或我发错了请求。有任何想法吗?谢谢。

+4

你如何修复它的任何更新?我有类似的问题。继续从Tumblr回来401,但不知道为什么。 –

+0

有同样的问题...你有没有设法解决它(以及如何)? – SimpleVar

回答

0

有在GitHub上,可以帮助您一个完整的例子...

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

+5

这没有帮助,因为它使用用户名/密码进行授权,而不是OAuth。 –

+0

我发现这[链接](https://www.codeproject.com/Tips/578418/Integrate-Tumblr-into-Csharp-NET-Website)很有帮助。它包含一个'Helper'类,您可以在其中轻松执行Oauth 1.0a,并且还可以通过GET和POST请求tumblr获取用户信息。希望这可以帮助某人 –