Integrate Twitter Social API into Django App

Application Programming Interface (API):

An Application Programming Interface is a set of commands, routines and tools to create software applications

Twython:

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

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

After Registering, get your applications ConsumerKey and Consumer Secret from 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, use those credentials to verify the user's identity.

Authentication using OAuth 1 and OAuth2

OAuth 1 is for user making requests for 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)

0Auth 1 is the most common type used for authentication

If your web application is having 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

After authorize you will get the oauth_verifier in the url with this oauth_verifier you can get oauth_token and oauth_token_secret 

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)

OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']

update_status(parameters):

update_status is used for updting the status of user on twitter

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
Create excel file, Insert image, Draw Bar Graphs in excel files in python using xlsxwriter Chaitanya Kattineni

Xlsxwriter is a python module through which we can write data to Excel 2007+ XLSX file format. In this blog post we learn to write ...

Continue Reading...
How to access development server publicly using Localtunnel Ashwin Kumar

We do need to expose our local server to hit call-back URLs while programming with other APIs. There is a tool called local tunnel from ...

Continue Reading...
Using Python xlwt generating CSV writer and Excel files Shirisha Gaddi

In most of the cases, you need to export the data from your database to different formats. In this post I will show you how ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties