Chapter 1. The Recipes

Using OAuth to Access Twitter APIs

Problem

You want to access your own data or another user’s data for analysis.

Solution

Take advantage of Twitter’s OAuth implementation to gain full access to Twitter’s entire API.

Discussion

Twitter currently implements OAuth 1.0a, an authorization mechanism expressly designed to allow users to grant third parties access to their data without having to do the unthinkable—doling out their username and password. Various third-party Python packages such as twitter (easy_install twitter) provide easy-to-use abstractions for performing the “OAuth dance,” so that you can easily implement client programs to walk the user through this process. In the case of Twitter, the first step involved is registering your application with Twitter at http://dev.twitter.com/apps where Twitter provides you with a consumer key and consumer secret that uniquely identify your application. You provide these values to Twitter when requesting access to a user’s data, and Twitter prompts the user with information about the nature of your request. Assuming the user approves your application, Twitter then provides the user with a PIN code for the user to give back to you. Using your consumer key, consumer secret, and this PIN code, you retrieve back an access token and access token secret that ultimately are used to get you the authorization required to access the user’s data.

Example 1-1 illustrates how to use the consumer key and consumer secret to do the OAuth ...

Get 21 Recipes for Mining Twitter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.