By JCaisip
This article is also available here.
Jonisium Experiment is currently testing DotNetOpenAuth, LinqToTwitter and Twitterizer to use on its new ASP.NET MVC project.
Currently Jonisium's Twi-Star API is using TwitterVB by DWroelands. TwitterVB is very well written Twitter Library easy enough to understand, implement and use for your VB.NET project.
The C-Sharp Twitter API Experiment - Initial Oauth Testing
Jonisium is using LinqToSql for all its queries so I like using it. LinqToTwitter is LINQ and it is very easy to understand the syntax.
LinqToTwitter is using DotNetOpenAuth OAuth authentication using the implementation of TokenManager. The WebFormsDemo which is included in the download package has the following public class under the App_code folder
public class InMemoryTokenManager : IConsumerTokenManager
This class holds the access tokens required for Oauth authorization. I am getting error trying to preview the sample code likely because I need to update some assembly codes in the web.config. Not good about fixing this problem I tried copying the codes to my MVC project. After copying the codes, I keep getting error because I used the latest DotNetOpenAuth dll. This lead me to test the DotNetOpenAuth implementation of TokenManager which is surprisingly different than what LinqToTwitter is using.
LinQToTwitter is using
auth = new WebOAuthAuthorization(InMemoryTokenManager.Instance, InMemoryTokenManager.AccessToken);
DotNetOpenAuth is using
var twitInstance = new WebConsumer(TwitterConsumer.ServiceDescription, this.TokenManager);
WebOauthAuthorization is using different model properties than WebConsumer and you can notice the parameters are different. I am not sure what Joe Mayo is thinking why he modified the Token Manager implementation of DotNetOpenAuth I think that makes his library dependent on a specific version of DotNetOpenAuth unless there is something beyond my knowledge right now.
The OAuthConsumer website example included in the download package works well after loading it on Visual Studio 2008 Express edition. Surpringly, I can get authenticated using the local host and Twitter is able to get back to my local host - how this is done is still beyond my knowledge of OAuth process but it is the only one out of 4 different libraries I tested that can do this without worrying of callback URL. With DotNetOpenAuth everything is working well.
The next challenge using the InMemoryTokenManager that came out with the download package is storing the access token and token secret for every users. This is going to be the same challenge if you use LinqToTwitter if you are not so experienced coder like me.
In order to reuse the tokens, I implemented TokenManager in order to get the access token and secret.
private JeTokenManager TokenManager
{
get
{
JeTokenManager tokenManager = (JeTokenManager)HttpContext.Application["TwitterTokenManager"];
//JeTokenManager tokenManager = (JeTokenManager)null;
if (tokenManager == null)
{
string consumerKey = _IAccount.GetAppStrParam("Twitter_ConsumerKey");
string consumerSecret = _IAccount.GetAppStrParam("Twitter_ConsumerKeySecret");
tokenManager = new JeTokenManager(consumerKey, consumerSecret, null, null);
HttpContext.Application["TwitterTokenManager"] = tokenManager;
}
return tokenManager;
}
}
In the Controller Action code the access tokens can be requested using
var twitInstance = new WebConsumer(TwitterConsumer.ServiceDescription, this.TokenManager);
// Is Twitter calling back with authorization?
var accessTokenResponse = twitInstance.ProcessUserAuthorization();
if (accessTokenResponse != null)
{
this.AccessToken = accessTokenResponse.AccessToken;
this.AccessTokenSecret = this.TokenManager.GetTokenSecret(accessTokenResponse.AccessToken);
this.ScreenName = accessTokenResponse.ExtraData["screen_name"];
if ((accessTokenResponse.ExtraData["user_id"]) != null)
{
twitModel.Id = Convert.ToInt32(accessTokenResponse.ExtraData["user_id"]);
}
// You can now save your tokens here. Now that you have access token and secret you can use twitter Library
// using something like Twitter.Update(token);
}
For additional information I have my own token model as follows
public class JeOauthTokenModel
{
public string ConsumerKey { get; set; }
public string ConsumerKeySecret { get; set; }
public string AccessToken{ get; set; }
public string AccessTokenSecret { get; set; }
}
The above code snipppet is enough to get me the access token and token secret so now I have the four ingredients of an Oauth process. ConsumerKey, ConsumerKeySecret, AccessToken and AccessTokenSecret.
Now I can use any methods of calling Twitter API using my own webservice or other Twitter API Libraries.
The next challenge in calling the API links directly is how to create an IEnumerable or IQueryable data so instead of spending time creating own model properties and XML or JASON codes I used open source libraries.
Back to LinqToTwitter, I cannot simply use this library using the tokens saved in my Server because of its modified use of TokenManager so I decided to look for alternative. So far I like how TwitterVB is created because it can easily be used with other OAuth implementation so I am looking for the same type of implementation.
After a few tries googling for a .net Twitter library, I came across Twitterizer. I quickly realized it is using the same method like TwitterVB something like twitter.update(token). Right away I knew it is something I like to use. Notice how I changed the implementation of TokenManager during the initial authorization
tokenManager = new JeTokenManager(consumerKey, consumerSecret, null, null);
I decided to use DotNetOpenAuth for my authentication because I can use the same libraries to enable OpenID and InfoCards in my site for future upgrades. Using DotNetOpenAuth for authentication I can now do the following call to TokenManager to make the authorization process quicker and if the tokenKey and tokenSecret are no longer valid you get the new one.
tokenManager = new JeTokenManager(consumerKey, consumerSecret, tokenKey, tokenSecret)
So now using Twitterizer I can easily do this
TwitterStatus twit = new TwitterStatus(token);
TwitterStatusCollection myHomeTimeLine = TwitterStatus.GetHomeTimeline(token);
ViewData["timeLine"] = CreateTimeLines(myHomeTimeLine);
During the experiment I also tested a code from Shannon Whitley here . The code really thought me how to manually call API's using HttpWebRequest method and it is a good alternative to using a standard OAuth for authentication.
I have concluded for now to use Twitterizer for the C-Sharp version of my ASP.NET MVC Project because it is simple to use much like the TwitterVB on my VB.NET MVC project. I decided to keep DotNetOpenAuth for authorization because that will allow me to do some experiment on OpenID in the future.
Thank you to all the authors of the libraries and codes I mentioned in this article.
Jonisium is currently developing its 'production' version of Jonisium Experiment using ASP.NET MVC and C-Sharp project. The site will focus on Twitter's add-on features to keep its social aspect but will put some focus in developing productivity tools for personal and business use.
Jonisium Experiment will remain as a live experimental site but l may change the format to reflect its 'non-production' presence on the internet.
More on the Jonisium road map in the upcoming blogs.