Linq2Twitter, Open Authentication module and Orchard CMS

#orchard #openauthentication

Posted by admin on September 01, 2019

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;

  public HomeController(IOAuthTwitterAuthorizer twitterAuthorizer, IOrchardServices orchardServices) {
   _twitterAuthorizer = twitterAuthorizer;
   _orchardServices = orchardServices;
  }

  ViewResult RetrieveTweets() {
   var twitterCtx = new TwitterContext(_twitterAuthorizer.GetAuthorizer(_orchardServices.WorkContext.CurrentUser));

   var tweets =
    from tweet in twitterCtx.Status
   where tweet.Type == StatusType.Friends
   select tweet;

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

   return null;
  }
 }
}

Hope this help people

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

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

Nick