使用Twitter API 1.1 oAuth验证并请求用户的时间表

时间:2013-06-12 14:22:14

标签: c# asp.net twitter webclient twitter-oauth

今天早上我收到了可怕的'Twitter REST API v1不再有效。请迁移到API v1.1。我的一些网站出现错误。

以前我一直在使用javascript / json来调用http://api.twitter.com/1/statuses/user_timeline.json吗?显示时间表。

由于这不再可用,我需要采用新的1.1 API流程。

我需要使用HttpWebRequest对象而不是第三方应用程序执行以下操作:

  1. 使用oauth密钥和秘密进行身份验证
  2. 进行经过身份验证的调用以撤回以显示用户时间线

2 个答案:

答案 0 :(得分:101)

以下是我在一个简单的例子中所做的工作。

我必须在Twitter上生成oAuth消费者密钥和秘密:

https://dev.twitter.com/apps/new

我首先对身份验证对象进行反序列化以获取令牌并返回以验证时间轴调用。

时间轴调用只是读取json,因为这就是我需要做的事情,你可能想要将它自己反序列化为一个对象。

我已经在https://github.com/andyhutch77/oAuthTwitterWrapper

创建了一个项目

更新 - 我更新了github项目以包含asp .net web app& mvc app示例演示和nuget安装。

// You need to set your own keys and screen name
var oAuthConsumerKey = "superSecretKey";
var oAuthConsumerSecret = "superSecretSecret";
var oAuthUrl = "https://api.twitter.com/oauth2/token";
var screenname = "aScreenName";

// Do the Authenticate
var authHeaderFormat = "Basic {0}";

var authHeader = string.Format(authHeaderFormat,
    Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" +
    Uri.EscapeDataString((oAuthConsumerSecret)))
));

var postBody = "grant_type=client_credentials";

HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Method = "POST";
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

using (Stream stream = authRequest.GetRequestStream())
{
    byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
    stream.Write(content, 0, content.Length);
}

authRequest.Headers.Add("Accept-Encoding", "gzip");

WebResponse authResponse = authRequest.GetResponse();
// deserialize into an object
TwitAuthenticateResponse twitAuthResponse;
using (authResponse)
{
    using (var reader = new StreamReader(authResponse.GetResponseStream())) {
        JavaScriptSerializer js = new JavaScriptSerializer();
        var objectText = reader.ReadToEnd();
        twitAuthResponse = JsonConvert.DeserializeObject<TwitAuthenticateResponse>(objectText);
    }
}

// Do the timeline
var timelineFormat = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=1&exclude_replies=1&count=5";
var timelineUrl = string.Format(timelineFormat, screenname);
HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(timelineUrl);
var timelineHeaderFormat = "{0} {1}";
timeLineRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, twitAuthResponse.token_type, twitAuthResponse.access_token));
timeLineRequest.Method = "Get";
WebResponse timeLineResponse = timeLineRequest.GetResponse();
var timeLineJson = string.Empty;
using (timeLineResponse)
{
    using (var reader = new StreamReader(timeLineResponse.GetResponseStream()))
    {
         timeLineJson = reader.ReadToEnd();
    }
}


public class TwitAuthenticateResponse {
    public string token_type { get; set; }
    public string access_token { get; set; }
}

答案 1 :(得分:2)

创建了一个仅限JS的解决方案,可以在不使用新API的情况下在您的网站上发布Twitter帖子 - 现在也可以指定推文的数量:http://goo.gl/JinwJ

相关问题