Created
December 25, 2014 15:08
-
-
Save parthrbhatt/4d7e7f4c0cae376cc727 to your computer and use it in GitHub Desktop.
OAuth with Twitter using tweepy for a Desktop application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This script shows how a Desktop application can use OAuth with twitter using tweepy (https://github.com/tweepy/tweepy). | |
Note: | |
- This example depends on tweepy. If you dont have it installed already, you can install it using: | |
pip install tweepy | |
- This example has been tested with tweepy 3.1.0 | |
''' | |
__author__ = "Parth Bhatt" | |
__email__ = "parthrbhatt@gmail.com" | |
__version__ = "1.0.0" | |
__license__ = "MIT" | |
import tweepy | |
CONSUMER_TOKEN = '<YOUR-CONSUMER-TOKEN>' | |
CONSUMER_SECRET = '<YOUR-CONSUMER-SECRET>' | |
class TwitterOAuthTool: | |
"""OAuth tool that allows a twitter user to authorize the app to access her | |
twitter account. | |
Usage: | |
tool = TwitterOAuthTool() | |
auth_url = tool.get_authorization_url() | |
# Request user to visit `auth_url` in a browser (or launch browser with this | |
# url) and authorize the app. Once the user authorizes the app, twitter will | |
# provide a PIN (a.k.a Verifier Code). | |
# Get the PIN from the user and use it in the API below | |
(token, secret) = tool.get_token_and_secret(verifier_code) | |
# You will also want to save the token & secret for future use. | |
if (token and secret): | |
me = tool.verify_authorization() | |
print 'User %s successfully authorized the app.' %me.screen_name | |
else: | |
print 'Failed to get the token and secret for the user.' | |
""" | |
def __init__(self): | |
self.auth = None | |
self.access_token = None | |
self.access_token_secret = None | |
def get_authorization_url(self): | |
"""Generates the authorizarion URL. | |
The user needs to visit the generated url, login to twitter and authorize | |
the app to access user's twitter account. | |
:Return: | |
- String contanining a URL or None in case of an error. | |
""" | |
redirect_url = None | |
self.auth = tweepy.OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET) | |
try: | |
redirect_url = self.auth.get_authorization_url() | |
except tweepy.TweepError, e: | |
print 'Error! Failed to get request token.' | |
return redirect_url | |
def get_token_and_secret(self, verifier_code): | |
"""Generate the access-token and access-token-secret for the user who | |
just authorized the application. | |
:Parameter: | |
- `verifier_code`: Verification Pin generated by twitter after the | |
user authorized the app. | |
:Return: | |
- `(token, secret)`: tuple containing key and secret. This should be | |
stored and can be used to construct a tweepy api object to | |
access user's twitter account. | |
""" | |
try: | |
self.auth.get_access_token(verifier_code) | |
except tweepy.TweepError: | |
print 'Error! Failed to get access token.' | |
return None | |
self.access_token = self.auth.access_token | |
self.access_token_secret = self.auth.access_token_secret | |
return (self.access_token, self.access_token_secret) | |
def verify_authorization(self): | |
"""Verify that the user authorizing the application has been done | |
successfully. | |
""" | |
auth = tweepy.OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET) | |
auth.set_access_token(self.access_token, self.access_token_secret) | |
api = tweepy.API(auth) | |
return api.me() | |
if '__main__' == __name__: | |
tool = TwitterOAuthTool() | |
auth_url = tool.get_authorization_url() | |
print 'Visit the following url in a web-browser & authorize this app to access your twitter account.' | |
print 'Once you are done authorizing, copy the PIN that twitter generates and paste it below.' | |
print '\nURL: %s' %auth_url | |
verifier_code = raw_input('PIN: ') | |
(token, secret) = tool.get_token_and_secret(verifier_code) | |
if (token and secret): | |
me = tool.verify_authorization() | |
print 'User @%s successfully authorized the app.' %me.screen_name | |
else: | |
print 'Failed to get the key and secret for the user.' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good, thk