Authenticating Users With Twitter OAuth

If you’ve ever played around with the Twitter API, you’ll know that many functions require authentication with either a username/password combination or OAuth. Soon Twitter will be turning off basic authentication for security reasons, in favor of the more complex OAuth protocol. There are plenty of benefits, for Twitter and for users and for developers, but the transition will be a bit of a pain.

Fortunately, Net.Tuts+ has put together a tutorial explaining how to implement OAuth authentication in a Twitter application.

It’s a more complicated than it should be to implement the authentication dance (but it’s very easy for the end user, thankfully). There is one plus to using one of the Twitter OAuth libraries: it’s easier to handle API requests. Need to get a user timeline? If you’re already authenticated, you can just do this:

$nettuts_timeline = $twitteroauth->get('statuses/user_timeline', array('screen_name' => 'nettuts'));

No worrying about cURL or file_get_contents(), the library’s classes take care of all of the boring stuff for you.