通过API 1.1使用Twitter的OAuth没有第三方库 [英] Using Twitter OAuth via API 1.1 without 3rd Party Library

查看:178
本文介绍了通过API 1.1使用Twitter的OAuth没有第三方库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的麻烦在使用GET状态/ user_timeline通过OAuth。

I am facing trouble in using GET statuses/user_timeline with OAuth.

我是新来Twitter和放大器;节目。大多数的帮助下,我的文档中看到的是POST。

I am new to twitter & programming. Most of the help I see in the documentation are for POST.

早些时候,我使用: http://api.twitter.com /1/statuses/user_timeline.json?screen_name=userid

现在根据新的API,我试图使用:的https: //api.twitter.com/1.1/statuses/user_timeline.json?screen_name=userid
但我得到远程服务器返回错误:(400)错误的请求。

Now based on new API, I am trying to use: https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=userid. But I get "The remote server returned an error: (400) Bad Request".

我取得了dev.twitter.com的应用程序。我也设法生成一个签名。现在,我不知道如何发布签名并得到输出。我使用C#Web表单.NET 3.5。我不能使用第三方库像twitterizer。

I made an application in dev.twitter.com. I also managed to generate a signature. Now i dont know how to post the signature and get the output. I using C# web forms .net 3.5. I cannot use a 3rd party library like twitterizer.

我跟在<一个线程href=\"http://stackoverflow.com/questions/10673158/get-twitter-public-timeline-jsonc-no-3rd-party-libraries\">Get叽叽喳喳公开时间表,JSON + C#,和两个跟进线程没有第三方库。事情错了。我假设它的东西与API 1.1做的。

I followed the thread at Get twitter public timeline, json+C#, no 3rd party libraries and the two follow up threads. Something is going wrong. I am assuming that its something to do with API 1.1.

推荐答案

在这里你去(改变更新我一节):

Here you go (change the "update me" section) :

        // oauth application keys
        var oauth_token = "update me";
        var oauth_token_secret = "update me";
        var oauth_consumer_key = "update me";
        var oauth_consumer_secret = "update me";

        // oauth implementation details
        var oauth_version = "1.0";
        var oauth_signature_method = "HMAC-SHA1";

        // unique request details
        var oauth_nonce = Convert.ToBase64String(
            new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
        var timeSpan = DateTime.UtcNow
            - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

        // message api details
        var status = "Updating status via REST API if this works";
        var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
        var screen_name = "updateme";
        // create oauth signature
        var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                        "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}";

        var baseString = string.Format(baseFormat,
                                    oauth_consumer_key,
                                    oauth_nonce,
                                    oauth_signature_method,
                                    oauth_timestamp,
                                    oauth_token,
                                    oauth_version,
                                     Uri.EscapeDataString(screen_name)
                                    );

        baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

        var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                "&", Uri.EscapeDataString(oauth_token_secret));

        string oauth_signature;
        using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
        {
            oauth_signature = Convert.ToBase64String(
                hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
        }

        // create the request header
        var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                           "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                           "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                           "oauth_version=\"{6}\"";

        var authHeader = string.Format(headerFormat,
                                Uri.EscapeDataString(oauth_nonce),
                                Uri.EscapeDataString(oauth_signature_method),
                                Uri.EscapeDataString(oauth_timestamp),
                                Uri.EscapeDataString(oauth_consumer_key),
                                Uri.EscapeDataString(oauth_token),
                                Uri.EscapeDataString(oauth_signature),
                                Uri.EscapeDataString(oauth_version)
                        );


        // make the request

        ServicePointManager.Expect100Continue = false;

        var postBody = "screen_name=" + Uri.EscapeDataString(screen_name);//
        resource_url += "?" + postBody;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";


        WebResponse response = request.GetResponse();
        string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();

这篇关于通过API 1.1使用Twitter的OAuth没有第三方库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆