This project is read-only.

Get Twitter Authentication from external module

As described here http://www.themayneissue.com/memorystream/linq2twitter-open-authentication-module-and-orchard-cms

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;
        }

        public 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 => Console.WriteLine(
                    "Friend: {0}\nTweet: {1}\n",
                    tweet.User.Name,
                    tweet.Text));

            return null;
        }
    }
}

Last edited Feb 27, 2011 at 8:08 PM by Jetski5822, version 2

Comments

Jetski5822 Apr 15, 2012 at 11:45 PM 
IOAuthTwitterAuthorizer has been renamed to IOAuthProviderTwitterAuthenticator