开发者

Posting on Tumblr using oAuth and C#

开发者 https://www.devze.com 2023-04-07 01:48 出处:网络
I\'m trying to develop a simple application to interact with Tumblr using v2 APIs. I\'m able to go past every step in the oAuth flow (thanks to the documentation found on Twitter) and obtain an author

I'm trying to develop a simple application to interact with Tumblr using v2 APIs. I'm able to go past every step in the oAuth flow (thanks to the documentation found on Twitter) and obtain an authorized and authenticated token and secret. But when it comes to posting, I get a 401 unauthorized error. This is the relevant code snippet:

    private void post_Click(object sender, System.Windows.RoutedEvent开发者_如何学运维Args 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);
        }
    }

The function is called upon clicking on a button and get the body text from a text box. The signing part is correct because it has been tested with other services (Twitter etc.) and it still provides valid signatures for Tumblr itself. The Authorization header is pretty much the same I've used to request a token and to authorize it, and the base string only contains the type of the content (text in this case) and the body, apart from the oauth_* standard parameters. I also checked the code with the tools available on hueniverse.com, but still nothing.

Probably I'm missing some HTTP header or I'm POSTing the wrong request. Any ideas? Thanks.


there is a full example on github that may help you...

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号