Consuming the Twitter REST API with Python

REST services can be used with standard HTTP Requests and, in any case, if the service requires it, the requests must contain specific headers that allow authentication and authorization processes.

Starting with this, there are several possibilities to create a Python script that can consume a REST service, for example using the urllib module or other libraries written by third parties such as urllib3 or requests.

There are other modules for working with Twitter from Python, such as Tweepy (https://github.com/tweepy/tweepy) and Python-Twitter (https://github.com/bear/python-twitter).

To access the Twitter API, it is necessary to have developer credentials. These credentials can be obtained from https://apps.twitter.com when creating a new application. The data that will be used includes the following:

  • Consumer key
  • Consumer secret
  • OAuth token
  • OAuth token secret

These keys will be needed to make the connection with the API through OAuth and allow our application to use our account, since to access certain functionalities of the API we will have to be logged in.

After ensuring the necessary authentication values correspond to the application previously created, the next step is to use the library to consume some of the REST services available in the Twitter API. To do this, the first step is to create a twitter.oauth.OAuth object.

If the authentication values are correct, the instance of the API class will contain all the methods necessary to consume the Twitter rest services.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset