Linq2Twitter, Open Authentication module and Orchard CMS

Note : The Open authentication module is a work in progress this may change, though I thought I would blog about it because I thought it was cool Smile Also all code is on Dev branch at the moment.

Linq2Twitter is a really cool project that allows you to easily interact with twitter, so how do you authenticate with this module without having to worry about authentication? (bit weird huh) In comes the OpenAuthentication module for Orchard CMS.

First you need to have a twitter association set up in Orchard, thus because IUser is required.

1. First set up Twitter association..

Click the ‘Associated Accounts’ under the Users section on the admin page. Next click Add Account

image

Then select ‘Twitter’ (yes the pic sucks at the moment)

image

This will redirect you to the twitter page where you can select ‘allow’ or ‘deny’

image

Select ‘Allow’ because you know my module is awesome. This will return you back to the orchard Admin page.

image

Yes the account is not displaying correctly. but that will be worked out…  Someone please log a bug Smile

- Account Set up

2. Next thing, over to Linq2Twitter. The Open Authentication module has an interface called IOAuthTwitterAuthorizer. This will allow you to get the authorizer for a particular user.

The key to getting the authorizer is to use the GetAuthorizer(User Here) method where you pass thru the IUser (hence why you need the user setup in orchard).

I have done a code sample below. Easy right??

using System;
using System.Linq;
using System.Web.Mvc;
using LinqToTwitter;
using NGM.OpenAuthentication.Core.OAuth;
using Orchard;

namespace NGM.Twitter.Controllers { public class HomeController : Controller { private readonly IOAuthTwitterAuthorizer _twitterAuthorizer; private readonly IOrchardServices _orchardServices;

    <span class="kwrd">public</span> HomeController(IOAuthTwitterAuthorizer twitterAuthorizer, IOrchardServices orchardServices) {
        _twitterAuthorizer = twitterAuthorizer;
        _orchardServices = orchardServices;
    }

    <span class="kwrd">public</span> ViewResult RetrieveTweets() {
        var twitterCtx = <span class="kwrd">new</span> TwitterContext(_twitterAuthorizer.GetAuthorizer(_orchardServices.WorkContext.CurrentUser));

        var tweets =
            from tweet <span class="kwrd">in</span> twitterCtx.Status
            <span class="kwrd">where</span> tweet.Type == StatusType.Friends
            select tweet;

        tweets.ToList().ForEach(
            tweet =&gt; Console.WriteLine(
                <span class="str">"Friend: {0}\nTweet: {1}\n"</span>,
                tweet.User.Name,
                tweet.Text));

        <span class="kwrd">return</span> <span class="kwrd">null</span>;
    }
}

}

Hope this help people

Codeplex project : http://orchardopenauth.codeplex.com/

p.s. don't forget to enable the Open Authentication module!!!

Nick

3 Comments

  • emubd said

    I like this solution.but i have gotten a better solution in other site this site below. Orchard is a free, open source, community-focused project aimed at delivering applications and reusable components on the ASP.NET platform.....more http://www.dotnetboss.com/2011/01/18/orchard-1-0-has-finally-arrived/

Leave a Comment

Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!