Date post: | 19-May-2015 |
Category: |
Technology |
Upload: | alexey-komissarouk |
View: | 1,149 times |
Download: | 1 times |
PennApps 2010
The Twitter APIA Brief 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
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
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
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)
So doesn’t this compromise its availability?
No.
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
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
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?
Answer: You don’t.
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
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
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?
Enter the Twitter API.
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
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...
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
Let’s get started.
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”
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
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...
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?
Enter OAuth.
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
But the implementation is a bit tricky. So we’ll take our
time.
Works like this
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
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
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?
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
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!
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
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
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...
Voilà!
That wasn’t so bad!
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
OnwardsHow can you go on from here?
Try out the different API calls
Timeline
User timeline
User info
Favorite tweets
Direct messages
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
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
Enter Birdfeed.
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
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
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
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
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.
What will you do with the Twitter API?
Something important, say...
...winning PennApps?
The end.