Created
February 12, 2013 18:45
-
-
Save hezhao/4772180 to your computer and use it in GitHub Desktop.
Twitter PIN-based authorization using tweepy
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
### 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 |
Add
callback='oob'
while creating thetweepy.OAuthHandler(consumer_key, consumer_secret, callback='oob')
so that Twitter produces the PIN numbers instead of redirecting to thecallback_uri
defined as part of the app (i.e.Consumer
) definition ondeveloper.twitter.com
.
You saved my weekend. Thank you so much!
Add
callback='oob'
while creating thetweepy.OAuthHandler(consumer_key, consumer_secret, callback='oob')
so that Twitter produces the PIN numbers instead of redirecting to thecallback_uri
defined as part of the app (i.e.Consumer
) definition ondeveloper.twitter.com
.
Thanks sir, you help me alot!
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
Add
callback='oob'
while creating thetweepy.OAuthHandler(consumer_key, consumer_secret, callback='oob')
so that Twitter produces the PIN numbers instead of redirecting to thecallback_uri
defined as part of the app (i.e.Consumer
) definition ondeveloper.twitter.com
.