How to Use Restful Web APIs in Python

This tutorial will introduce you to web APIs and teach you how to use the requests Python library to fetch and update information in web APIs. You will also learn how to interact with the Twitter API as a working example.

Introduction to Web APIs

An API (Application Programming Interface) is a framework for building HTTP services that can be consumed by a wide variety of clients. Web APIs use HTTP protocol to handle requests between the client and the web server.

Some of the most common APIs that enable developers to integrate and use their infrastructure include:

  1. Google APIs
  2. Twitter API
  3. Amazon API
  4. Facebook API

One of the most important reasons to use an API as opposed to other static data sources is because it’s real time. For example, the Twitter API we are going to use will fetch real-time data from the social network.

Another advantage is that the data keeps changing, so if you were to download it at intervals, it would be time-consuming.

Using the Requests Library

In order to use an API, you will need to install the requests Python library. Requests is an HTTP library in Python that enables you to send HTTP requests in Python.

Install Requests

In your terminal, type:

To check if the installation has been successful, issue the following command in your Python interpreter or the terminal:

If there are no errors, the installation has been successful.

How to Get Information From a Web API

The GET method is used to get information from a web server. Let’s see how to make a GET request to get GitHub’s public timeline.

We use the variable req to store the response from our request.

Now that we have made a request to the GitHub timeline, let’s get the encoding and the content contained in the response.

Requests has a built-in JSON decode which you can use to get the response of a request in JSON format.

How to Create and Update Information on the Web API

The POST and PUT methods are both used to create and update data. Despite the similarities, it’s important to note that using a POST request to update data will result in two entries in the data store if two identical items are submitted.

Create data (POST request):

Update data (PUT request):

Working With the Twitter REST API

In this section, you are going to learn how to obtain Twitter API credentials, authenticate to the Twitter API, and interact with the Twitter API using Python. 

You will also be able to retrieve information from public Twitter accounts, like tweets, followers, etc.

Authenticating With Twitter

We need to authenticate with the Twitter API before we can interact with it. To do this, follow the following steps:

  1. Go to the Twitter Apps page.
  2. Click on Create New App (you need to be logged in to Twitter to access this page). If you don’t have a Twitter account, create one.
Twitter App Page

3.  Create a name and description for your app and a website placeholder.

Creating a Twitter Application

4. Locate the Keys and Access Tokens Tab and create your access token.

Twitter Access Tokens

5. You need to take note of the Access token and Access Token secret since you will need them for the authentication process.

6. You also need to take note of the Consumer Key and Consumer Secret.

There are a few libraries that we can use to access the Twitter API, but we are going to use the python-twitter library in this tutorial.

Install python-twitter

To install python-twitter, use:

The Twitter API is exposed via the twitter.Api class, so let’s create the class by passing our tokens and secret keys:

Replace your credentials above and make sure they are enclosed in quotes, i.e. consumer_key=‘xxxxxxxxxx’, …)

Querying Twitter

There are many methods of interacting with the Twitter API, including:

To get data from Twitter, we are going to make an API call with the help of the api object we created above.

We will do the following:

  1. Create a user variable and set it equal to a valid Twitter handle (username).
  2. Call the GetUserTimeline() method on the api object and pass in the following arguments.

  • a valid Twitter handle
  • the number of tweets you want to retrieve (count)
  • a flag to exclude retweets (this is done using include_rts = false)

Let’s get the latest tweets from the Envato Tuts+ Code timeline, excluding retweets.

The GetUserTimeline() method will return a list of the latest 30 tweets, so we loop through the list and print the most important information (content) from each tweet.

To retrieve followers, we use the GetFriends() method.

Output

Conclusion

Twitter’s API can be used to a great extent in data analytics. It can also be used in complex big data problems and authenticating apps. Read more about the Twitter API at the Twitter developers site.

Also, don’t hesitate to see what we have available for sale and for study in the marketplace, and don’t hesitate to ask any questions and provide your valuable feedback using the feed below.


Source: Nettuts Web Development