Thursday 16 May 2013

Format Twitter Feed Create Date

Here's how to format the Twitter Date to the C# .net DateTime datatype. You will need to use "using System.Globalization;"

string date = [YourTwitterDateHere];

DateTime twitterDate = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);

Why they have this format is beyond me, but this is how you can use it for what ever purpose you require in your project. Enjoy!

Wednesday 15 May 2013

Twitter API 1.1 oAuth - Cut the crap and get it working :D

After lots of research, piecing together of all sorts of code, I finally managed to get a working version of oAuth API 1.1 in twitter. It still need a lot of refactoring, but you will get the idea. Its not pretty, but it works :D When I get time I might improve this and post a cleaner version ;) It turns out its is quite fussy about how you send the request with the request headers. This also uses JSON.net to parse the and dynamic type is quite interesting and is something I havent used before. This will allow you to databind to control like any other datasource which is useful and to return just the latest tweet you can literally just change the count to 1. Anyway enough waffle, you can download the source from: Download Sourcecode Or you can see the class below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TwitterTest;

namespace TwitterControl
{
public partial class TwitterControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// Setup Request
Twitter.ScreenName = "Test"; // This can be any twitter account
Twitter.Count = 20;

// These can be found in your developers seciton of twitter dev.twitter.com and in your applications
Twitter.oAuthConsumerKey = "";
Twitter.oAuthConsumerSecret = "";
Twitter.oAuth_token = "";

// Generate Random Base 64 Code
Twitter.oAuth_nonce = Twitter.generateNonce();

// Signature Method
Twitter.oAuth_signature_method = "HMAC-SHA1";

// Timestamp
Twitter.oAuth_timestamp = DateTime.Now.ToString();

// API Version
Twitter.oAuth_version = "1.1";

twitterRepeater.DataSource = Twitter.RetrieveTweets();
twitterRepeater.DataBind();
}
}
}

There is an issue with adding count, it has to be added to base string, in due course I will update this, its just finding the time :\