48
votes

This morning I have received the dreaded 'The Twitter REST API v1 is no longer active. Please migrate to API v1.1.' error in a few of my web sites.

Previously I have been using javascript/json to make these calls to http://api.twitter.com/1/statuses/user_timeline.json? to display a timeline.

As this is no longer available I need to adopt the new 1.1 API process.

I need to do the following using HttpWebRequest objects not a 3rd party application:

  1. Authenticate using oauth key and secret
  2. Make an authenticated call to pull back to display users timeline
2
A mixture, I have traditional and mvc.hutchonoid
Have you seen this? There's also a list of third party libraries on the twitter dev site - there are a few C# ones.Jimbo
@Jimbo Hi, yes thanks. I need to avoid 3rd party libraries. The reason being that I need to use it in many different type of applications (mvc and traditional apps), CMS, portals etc. I've almost done it, I'll post the answer if it works.hutchonoid
@hutchonoid Is it possible to do a keyword search for all user in a WebForm ???Phill Healey
@PhillHealey Yes, I believe you can. Basically anything that is available from the twitter api can be exposed although you may have to extend/modify the source on github.hutchonoid

2 Answers

103
votes

Here is what I did to get this working in a simple example.

I had to generate an oAuth consumer key and secret from Twitter at:

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

I deserialized the authentication object first to get the token and type back in order to authenticate the timeline call.

The timeline call simply reads the json as that is all I need to do, you may want to deserialize it yourself into an object.

I have created a project for this at : https://github.com/andyhutch77/oAuthTwitterWrapper

Update - I have updated the github project to include both asp .net web app & mvc app example demos and nuget install.

// 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; }
}
2
votes

Created a JS only solution to get Twitter posts on your site without using new API - can now specify number of tweets too: http://goo.gl/JinwJ