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”)

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
Latest Comments
Related Articles
Customize and Embed Vimeo Videos using Python Requests. Dinesh Deshmukh

Using python requests and vimeo endpoints it becomes very easy and simple to upload our videos and customize them.

Vimeo Access token:
1. Create ...

Continue Reading...
Python Web Scraping with Beautiful soup Sandeep Jagata

Download all One Piece animation series episodes by scraping site using BeautifulSoup python library.

Continue Reading...
Debugging in Python Ramya Ambati

When something goes wrong with your code instead of using standard debugging techniques such as print statements use debugging tools. I found two great tools ...

Continue Reading...