Skip to content

Instantly share code, notes, and snippets.

@hezhao
Created February 12, 2013 18:45
Show Gist options
  • Save hezhao/4772180 to your computer and use it in GitHub Desktop.
Save hezhao/4772180 to your computer and use it in GitHub Desktop.
Twitter PIN-based authorization using tweepy
### See PIN-based authorization for details at
### https://dev.twitter.com/docs/auth/pin-based-authorization
import tweepy
consumer_key=<your_app_consumer_key>
consumer_secret=<your_app_consumer_secret>
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# get access token from the user and redirect to auth URL
auth_url = auth.get_authorization_url()
print 'Authorization URL: ' + auth_url
# ask user to verify the PIN generated in broswer
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
print 'ACCESS_KEY = "%s"' % auth.access_token.key
print 'ACCESS_SECRET = "%s"' % auth.access_token.secret
# authenticate and retrieve user name
auth.set_access_token(auth.access_token.key, auth.access_token.secret)
api = tweepy.API(auth)
username = api.me().name
print 'Ready to post to ' + username
@musically-ut
Copy link

Add callback='oob' while creating the tweepy.OAuthHandler(consumer_key, consumer_secret, callback='oob') so that Twitter produces the PIN numbers instead of redirecting to the callback_uri defined as part of the app (i.e. Consumer) definition on developer.twitter.com.

@nsauter
Copy link

nsauter commented Jun 28, 2020

Add callback='oob' while creating the tweepy.OAuthHandler(consumer_key, consumer_secret, callback='oob') so that Twitter produces the PIN numbers instead of redirecting to the callback_uri defined as part of the app (i.e. Consumer) definition on developer.twitter.com.

You saved my weekend. Thank you so much!

@w1th0ut
Copy link

w1th0ut commented Oct 3, 2021

Add callback='oob' while creating the tweepy.OAuthHandler(consumer_key, consumer_secret, callback='oob') so that Twitter produces the PIN numbers instead of redirecting to the callback_uri defined as part of the app (i.e. Consumer) definition on developer.twitter.com.

Thanks sir, you help me alot!

@mesoclever
Copy link

Update Python 3.9.8, Tweepy 4.40:

import tweepy

# api credentials 
auth = tweepy.OAuthHandler('api key', 'api secret','oob')

# get access token from the user and redirect to auth URL
auth_url = auth.get_authorization_url()
print('Authorization URL: ' + auth_url)

# ask user to verify the PIN generated in broswer
verifier = input('PIN: ').strip()
auth.get_access_token(verifier)
print('ACCESS_KEY = "%s"' % auth.access_token)
print('ACCESS_SECRET = "%s"' % auth.access_token_secret)

# authenticate and retrieve user name
auth.set_access_token(auth.access_token, auth.access_token_secret)
api = tweepy.API(auth)
user = api.verify_credentials()
print('Name: ' + str(user.name))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment