You do need a Twitter account, and you need to obtain a "consumer key" and "consumer secret". You do that by creating an App at https://dev.twitter.com/apps.
Here's a method in an IHttpHandler that you could adapt once you have those. It provides the same functionality to client side script that the https://api.twitter.com/1/statuses/user_timeline.json endpoint did. It requests the 1.1 version of that data after authenticating, so it will only work for your app.
For doing anything more complex than getting tweets, a proper OAuth handshake should be used - try the TweetSharp library: https://github.com/danielcrenna/tweetsharp
The code below follows the process described at https://dev.twitter.com/docs/auth/application-only-auth, but it does not do all the SSL checks described here - https://dev.twitter.com/docs/security/using-ssl.
public void ProcessRequest(HttpContext context)
{
// get these from somewhere nice and secure...
var key = ConfigurationManager.AppSettings["twitterConsumerKey"];
var secret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
var server = HttpContext.Current.Server;
var bearerToken = server.UrlEncode(key) + ":" + server.UrlEncode(secret);
var b64Bearer = Convert.ToBase64String(Encoding.Default.GetBytes(bearerToken));
using (var wc = new WebClient())
{
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
wc.Headers.Add("Authorization", "Basic " + b64Bearer);
var tokenPayload = wc.UploadString("https://api.twitter.com/oauth2/token", "grant_type=client_credentials");
var rgx = new Regex("\"access_token\"\\s*:\\s*\"([^\"]*)\"");
// you can store this accessToken and just do the next bit if you want
var accessToken = rgx.Match(tokenPayload).Groups[1].Value;
wc.Headers.Clear();
wc.Headers.Add("Authorization", "Bearer " + accessToken);
const string url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=YourTwitterHandle&count=4";
// ...or you could pass through the query string and use this handler as if it was the old user_timeline.json
// but only with YOUR Twitter account
var tweets = wc.DownloadString(url);
//do something sensible for caching here:
context.Response.AppendHeader("Cache-Control", "public, s-maxage=300, max-age=300");
context.Response.AppendHeader("Last-Modified", DateTime.Now.ToString("r", DateTimeFormatInfo.InvariantInfo));
context.Response.AppendHeader("Expires", DateTime.Now.AddMinutes(5).ToString("r", DateTimeFormatInfo.InvariantInfo));
context.Response.ContentType = "text/plain";
context.Response.Write(tweets);
}
}
You could:
- store the value obtained for accessToken for later use, you don't need to get it every time.
- Pass through parameters on the query string - I think user_timeline.json accepts the same parameters as the v1 version.
- set cache-related HTTP headers as appropriate - it's essential you cache this response to avoid running into request limits.
- Deserialise the JSON on the server and do something different with it. But if you're doing that, maybe just use TweetSharp directly. This is really for providing the same JSON data to client side script that previously worked with API v1.