【问题标题】:Posting on Tumblr using oAuth and C#使用 oAuth 和 C# 在 Tumblr 上发帖
【发布时间】:2011-11-23 07:55:21
【问题描述】:

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

    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 本身提供了有效的签名。 Authorization 标头与我用来请求令牌和对其进行授权的标头几乎相同,并且基本字符串仅包含内容的类型(在本例中为文本)和正文,除了 oauth_* 标准参数。我还使用 hueniverse.com 上提供的工具检查了代码,但仍然没有。

可能我缺少一些 HTTP 标头或者我发布了错误的请求。有任何想法吗?谢谢。

【问题讨论】:

  • 关于您如何修复它的任何更新?我有一个类似的问题。不断从 Tumblr 收到 401,但不知道为什么。
  • 遇到同样的问题...您是否设法解决(以及如何解决)?

标签: c# text oauth tumblr posting


【解决方案1】:

github上有一个完整的例子可以帮助你...

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

【讨论】:

  • 这无济于事,因为它使用用户名/密码进行授权,而不是 OAuth。
  • 我发现这个link 很有帮助。它包含一个Helper 类,您可以在其中轻松执行 Oauth 1.0a,还可以向 tumblr 发出 GET 和 POST 请求以获取用户信息。希望这可以帮助某人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 2011-06-18
相关资源
最近更新 更多