51
PennApps 2010 The Twitter API A Brief Tour

Jordan Kay's Twitter API tour

Embed Size (px)

DESCRIPTION

Jordan Kay (jordanekay)'s Twitter API guided tour for PennApps 2010.

Citation preview

Page 1: Jordan Kay's Twitter API tour

PennApps 2010

The Twitter APIA Brief Tour

Page 2: Jordan Kay's Twitter API tour

What is Twitter?

Twitter is simple

Subscribes to philosophy, “Do one thing and do it well”

Lets you share and discuss your thoughts through a community you design

Page 3: Jordan Kay's Twitter API tour

How is it used?

Any device (phone, tablet, PC...)

Any method (web, app, SMS...)

Tweets are your thoughts. Think them, tweet them!

Have something to share? Send out a tweet.

Should be easy, seamless, ubiquitous

Page 4: Jordan Kay's Twitter API tour

But wait!

Isn’t Twitter a website? Facebook is a website...

You’re thinking too small

Twitter is a mode of communication!

Talking, writing, calling, emailing, texting, and now, tweeting

Each has its own purpose, properties, and place

Page 5: Jordan Kay's Twitter API tour

There’s just one problem...Twitter is a web service

Tweets travel from you to your followers over the good old World Wide Web

Not through paper

Not through your voice

But through your very own, personal Internets

Or whatever you decide to call it (not a truck)

Page 6: Jordan Kay's Twitter API tour

So doesn’t this compromise its availability?

Page 7: Jordan Kay's Twitter API tour

No.

Page 8: Jordan Kay's Twitter API tour

The Internet is everywhereMore and more people have access to the Internet wherever they go

From their desk

From their lap

From their phone, on the go

Always on, always there

Most people take this very fact for granted

Page 9: Jordan Kay's Twitter API tour

Ubiquity

So I think I see what you mean

With the ubiquity of the web, a web service becomes a viable form of communication...

...not constrained by any context

Page 10: Jordan Kay's Twitter API tour

UbiquityStill, something’s off here...

I can access the web from anywhere, sure

But is logging on to a website the best way to share my thoughts?

How can Twitter be so natural and seamless if I need a website to use it?

Page 11: Jordan Kay's Twitter API tour

Answer: You don’t.

Page 12: Jordan Kay's Twitter API tour

The Internet is everywhere

Again, you’re thinking too small

Twitter uses the Internet to work

But to the user, with ubiquitous web access, this is just a minor implementation detail

No one really cares how calls work, or how texts work, or even how talking works

They just want them to work

Page 13: Jordan Kay's Twitter API tour

The Internet is everywhereBut the user does care about one thing:

How to interact with the device to use the service

Some sort of interface is required

But not just any

Different form factors call for different interfaces

Page 14: Jordan Kay's Twitter API tour

UbiquitySo the Twitter service itself must be interface agnostic

The data itself must be ubiquitous

Same data, same ways to use it, through any method of interaction

How do we solve this problem?

Page 15: Jordan Kay's Twitter API tour

Enter the Twitter API.

Page 16: Jordan Kay's Twitter API tour

What is an API?

API stands for “Application Programming Interface”

It’s an interface for your interface

How does a user interact with an application?

Through its user interface

How does the application interact with a service?

Through an API

Page 17: Jordan Kay's Twitter API tour

Too good to be true?So Twitter lets you access and act on all the data hosted by the service?

For the most part, yes

You just choose how you want to use it

But does it really work well?

You be the judge

Twitter certainly thinks it does...

Page 18: Jordan Kay's Twitter API tour

Sounds great!

I want to build on top of Twitter

What cool things can I do with it?

Integrate Twitter into your app...

...or your site...

...or your device

I want to take it for a spin

Page 19: Jordan Kay's Twitter API tour

Let’s get started.

Page 20: Jordan Kay's Twitter API tour

Let’s get startedWe’ll start with something simple

The public timeline, accessible to anyone

www.twitter.com/public_timeline

If this is your first exposure to Twitter, it gets better, I promise

Most of this stuff is useless...

...but so is “Hello World”

Page 21: Jordan Kay's Twitter API tour

Hello, Twitter APILet’s make our first API call to get the public timeline

You’ve probably used a UNIX-like terminal on your computer

For some people, it’s all they use

Fire it up, and run the following command:

curl http://twitter.com/statuses/public_timeline.rss

Page 22: Jordan Kay's Twitter API tour

Hello, Twitter APINeat!

Almost instantaneously, we’re presented with the public timeline in beautiful XML

Let’s try something else

curl -u username:password http://twitter.com/statuses/friends_timeline.xml

Oops...

Page 23: Jordan Kay's Twitter API tour

It’s not that easy...This used to work...

...but there were some downsides

Left user credentials exposed

Limited in number of requests

And what if the user changes his password...

...or changes his mind about the app?

Why not something better?

Page 24: Jordan Kay's Twitter API tour

Enter OAuth.

Page 25: Jordan Kay's Twitter API tour

What is OAuth?

Stands for “Open Authorization”

Uses tokens instead of credentials

Once you request a token with your credentials, you’re good to go

It’s that easy

Page 26: Jordan Kay's Twitter API tour

But the implementation is a bit tricky. So we’ll take our

time.

Page 27: Jordan Kay's Twitter API tour

Works like this

Page 28: Jordan Kay's Twitter API tour

App RegistrationOAuth is a little more “legit”

Want your application to access Twitter?

You’ll have to register it first

Let’s do this now

Hit up http://twitter.com/apps/new

I’ll walk you through

Page 29: Jordan Kay's Twitter API tour

Consumer Key and SecretAlright, we’ve registered our app

Let’s look at some of the information Twitter has provided us about it

We’ll focus on two, which we use to sign our requests to Twitter

Consumer Key

Consumer Secret

Page 30: Jordan Kay's Twitter API tour

Getting a Request Token

So far so good

The script that gets are request token is getreqtok.php

So let’s run it via the Terminal

cd /path/to/the/folder

php getreqtok.php

Did it work?

Page 31: Jordan Kay's Twitter API tour

Getting a Request Token

What did we get?

A request token...

...and its accompanying secret

Plus an authorization URL

So copy that URL and allow your new Twitter client to access your Twitter account

Page 32: Jordan Kay's Twitter API tour

OAuth VerificationThis should give you an OAuth verification number

Copy it down...

...and enter it into getacctok.php

What else do we need here?

Our request token and secret

Now run the script

Here’s your access token!

Page 33: Jordan Kay's Twitter API tour

Access Token

That’s it!

Really

Save this token somewhere, and you’re authenticated for good

So how do we use it?

Just include it with every API call you make

Speaking of API calls, let’s try one

Page 34: Jordan Kay's Twitter API tour

Making an API CallLet’s pick something simple

Let’s try posting a tweet

Got our access token? Good

Open up tweet.php and find the API call

statuses/update in post_tweet

We’ll have to send our token along with every call

Input your access token and its secret at the top

Page 35: Jordan Kay's Twitter API tour

Posting a TweetWe’re almost there!

Change $tweet to the tweet you want to send

And just run the script

php tweet.php

Now refresh your Twitter account (using an interface of your choosing..), and...

Page 36: Jordan Kay's Twitter API tour

Voilà!

Page 37: Jordan Kay's Twitter API tour

That wasn’t so bad!

Page 38: Jordan Kay's Twitter API tour

OnwardsWe’ve just scratched the surface

Practically anything you can think of, you can do with the API

Go crazy!

But read the documentation first (it’s good)

http://dev.twitter.com/doc

Page 39: Jordan Kay's Twitter API tour

OnwardsHow can you go on from here?

Try out the different API calls

Timeline

User timeline

User info

Favorite tweets

Direct messages

Page 40: Jordan Kay's Twitter API tour

Onwards

Or try out different interfaces and form factors

Something like what we’ve done using PHP would be great for a web app

But what about a desktop client?

Or a phone client?

The API’s got you covered

Page 41: Jordan Kay's Twitter API tour

Case Study: Birdfeed

I’m using the Twitter API myself

The iPhone has some great Twitter apps

But I think I can come up with something better

Page 42: Jordan Kay's Twitter API tour

Enter Birdfeed.

Page 43: Jordan Kay's Twitter API tour

MGTwitterEngine

iPhone applications are written in Cocoa, a framework usually used with Objective-C

With Cocoa, using the Twitter API directly is a bit unruly

With the open source MGTwitterEngine, developed by Matt Gemmell, you get a natural, Cocoa-like way to use the API

Page 44: Jordan Kay's Twitter API tour

MGTwitterEngine

The OAuth token flow works almost exactly the same

But uses an interface within the app for logging in

Once the user logs in, the token is retrieved behind the scenes

The token is saved, and that’s it for authentication

Page 45: Jordan Kay's Twitter API tour

MGTwitterEngine

All of the API calls are performed with a traditional Cocoa message passing syntax to an instance of your Twitter engine

For example, this will return a list of tweets from the user’s home timeline, ready to be displayed

Page 46: Jordan Kay's Twitter API tour

Case Study: BirdfeedBirdfeed is all about creating the best designed Twitter experience on the iPhone

“Design isn’t just how it looks...it’s how it works.”

Ways to make this happen

Intuitive, frictionless “pick up and do” interface

No meaningless cruft (shortened links, @usernames)

Communication is human, and Twitter should be too

Page 47: Jordan Kay's Twitter API tour

Case Study: BirdfeedWhat’s cool about this?

Ironically, the Twitter API was written in such a way that using it is what I care about the least

The high level of abstraction lets me not even think about how I’m interacting with data

Instead, gives me full focus on creating a user experience I think people will really love.

Page 48: Jordan Kay's Twitter API tour

What will you do with the Twitter API?

Something important, say...

Page 49: Jordan Kay's Twitter API tour

...winning PennApps?

Page 50: Jordan Kay's Twitter API tour

The end.

Page 51: Jordan Kay's Twitter API tour