14.01.2015 Views

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

whose timeline we’ll search for images<br />

- A Submit button to post the screen name to the server<br />

- A series of divs that will be laid out dynamically with the help<br />

of CSS.<br />

<br />

<br />

The markup to achieve this is quite simple and shown below:<br />

<br />

<br />

@using (Html.BeginForm(“Index”, “Home”, FormMethod.<br />

Post))<br />

{<br />

Get Recent Tweets by<br />

<br />

<br />

<br />

}<br />

<br />

@foreach (var item in Model)<br />

{<br />

if (!string.IsNullOrEmpty(item.MediaUrl))<br />

{<br />

<br />

<br />

<br />

}<br />

}<br />

<br />

<br />

<br />

The Controller and Hooking up with Twitter<br />

To keep the example simple, we will cut some corners here and<br />

instead of splitting our implementation up into the standard<br />

multiple layers, we’ll build a ‘fat controller’ instead.<br />

Step 8: Add a new Empty MVC Controller called HomeController<br />

OAuth Recap<br />

b. LinqToTwitter will check the SessionStateCredential store to<br />

see if it has a valid credential. If not, it will automatically redirect<br />

the user to Twitter’s website for Authentication. Once the user has<br />

authenticated and provided permissions for our App to access<br />

their Twitter account, we’ll get re-directed back to our application<br />

(all this is handled transparently by LinqToTwitter).<br />

c. We have not built ANY backend to store Twitter’s<br />

Authentication Token or the user’s id, so every time our session<br />

expires, we’ll have to Authenticate with Twitter again. This is just<br />

to keep things simple, in a real world application you should store<br />

the Authentication Token and reuse it till it gets revoked.<br />

The Controller Implementation<br />

The sole job for the Controller in our app is to return a list of<br />

TweetViewModel objects that is then used to render the View.<br />

Step 9: We start the Controller implementation by declaring the<br />

following fields<br />

private IOAuthCredentials credentials = new<br />

SessionStateCredentials();<br />

private MvcAuthorizer auth;<br />

private TwitterContext twitterCtx;<br />

All three types (or Interfaces) are defined in the LinqToTwitter<br />

namespace. The first one is a SessionStateCredentials() instance<br />

is the container for the Twitter provided authentication token. It’s<br />

valid once user has approved our app to connect to Twitter.<br />

The MvcAuthorizer takes care of the checking if the credentials<br />

from above are valid and if not, redirects to Twitter, intercepts the<br />

login success and stores the credentials in the Session.<br />

TwitterContext is like a DB Context, as in it’s the library used for<br />

querying Twitter just as if it were a database.<br />

Before we get into the Controller implementation, a quick recap of<br />

how we’ll use LinqToTwitter:<br />

a. LinqToTwitter will require a ConsumerKey and ConsumerSecret<br />

for our application. We have to get this from http://dev.twitter.com<br />

. You can refer to our previous editions to see how we can create<br />

an application for ourselves on Twitter. Once you have the keys,<br />

add them to the appSetting section in the web.config as follows:<br />

Step 10: With the field initialization done, we are ready to get<br />

data from Twitter and send it to the Index View. Core of the code is<br />

in the GetTweets method.<br />

private ActionResult GetTweets(string screenName)<br />

{<br />

if (credentials.ConsumerKey == null || credentials.<br />

ConsumerSecret == null)<br />

{<br />

DNcmagazine www.dotnetcurry.com | 51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!