Integrate Twitter Social API into Django App

Reading Time : ~ .

Application Programming Interface (API):

An Application Programming Interface(API) is a set of protocols, routines and tools for building software applications.

Twython:

Twython is the Python library providing an easy (and up-to-date) way to access Twitter data.

First, you’ll want to head over to https://dev.twitter.com/apps  and register an application.

After you register, grab your applications Consumer Key and Consumer Secret from the application details tab.

Now you’re ready to start authentication!

Authentication:

Authentication is the process of verifying the identity of a user by obtaining some sort of credentials and using those credentials to verify the user's identity.

Twython offers support for both OAuth 1 and OAuth 2 authentication.

OAuth 1 is for user authenticated calls (tweeting, following people, sending DMs, etc.)

OAuth 2 is for application authenticated calls (when you don’t want to authenticate a user and make read-only calls to Twitter, i.e. searching, reading a public users timeline)

The most common type of authentication is Twitter user authentication using OAuth 1.

If you're a web app planning to have users sign up with their Twitter account and interact with their timelines, updating their status, OAuth 1 authentication is used

If you’re not interested in authenticating a user and plan on making read-only calls, OAuth2 authentication is used

Authourization:

Authorization is the process of allowing an authenticated users to access the resources by checking whether the user has access rights to the system. Authorization helps you to control access rights by granting specific permissions to an authenticated user.

OAuth 1 (User Authentication)

First you should import Twython

from twython import Twython

Now, you should create a Twython instance with your consumer key and consumer secret

APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'
twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')

Only pass callback_url to get_authentication_tokens if your application is a Web Application

For Desktop and Mobile Applications callback_url is not required

From the auth variable, save the oauth_token_secret.

OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
Now,Send the user to the authentication url, you can obtain it by accessing
auth['auth_url']

Handling the Callback

After they authorize your application to access some of their account details, they'll be redirected to the callback url you specified in get_authentication_tokens

You'll want to extract the oauth_verifier from the url.

oauth_verifier = request.GET['oauth_verifier']

Now that you have the oauth_verifier stored to a variable, you’ll want to create a new instance of Twython and grab the final user tokens

twitter = Twython(APP_KEY,APP_SECRET,OAUTH_TOKEN,OAUTH_TOKEN_SECRET)
final_step = twitter.get_authorized_tokens(oauth_verifier)
Once you have the final user tokens, store them in a database
OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']

update_status(parameters):

Updates the authenticating user’s current status on twitter, also known as tweeting

parameters:

status(required): The text of your status updates, typically up to 140 characters.

in_reply_to_status_id(optional): The ID of an existing status that the update is in reply to.

Example:

twitter = Twython(TW_CONSUMER_KEY, TW_CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
update = twitter.update_status(status=”Your Status Here”)
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Sending SMS, MMS using Twilio. Chaitanya Kattineni

A simple Tutorial on sending SMS and MMS in python using Twilio. In this tutorial you will learn how to send SMS, MMS and checking ...

Continue Reading...
Generating PDF Files in Python using xhtml2pdf Siva Chittamuru

There are many ways for generating PDF in python. In this post I will be presenting PDF files generation with xhtml2pdf.

xhtml2pdf: xhtml2pdf is a ...

Continue Reading...
How to implement Case Insensitive CSV DictReader in python Chaitanya Kattineni

In general use cases we upload the CSV files to the system to store huge amount of data by uploading single file. For example in ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties