-
-
Save hezhao/4772180 to your computer and use it in GitHub Desktop.
### 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 |
Update for Python 3.5.2 and Tweepy 3.5.0:
### See PIN-based authorization for details at
### https://dev.twitter.com/docs/auth/pin-based-authorization
import os
import tweepy
consumer_key = os.environ['KEY']
consumer_secret = os.environ['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 = 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)
username = api.me().name
print('Ready to post to ' + username)
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
.
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))
Thanks, still works its just now: