Created
May 4, 2016 15:10
-
-
Save HJulio/2693779bc3da142def1c0a40d1d08e8b to your computer and use it in GitHub Desktop.
Tweepy auth saving tokens
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
import tweepy, os | |
def login(): | |
#Tokens | |
CONSUMER_TOKEN = '' | |
CONSUMER_SECRET = '' | |
auth = tweepy.OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET) | |
ACC_TOKEN_SV = os.path.expanduser('./._twitter_keys') | |
if not os.path.exists(ACC_TOKEN_SV): | |
try: | |
print("Auth. URL - Write here your PIN after login\n") | |
redirect_url = auth.get_authorization_url() | |
except tweepy.TweepError: | |
print("Error! Token ") | |
print(redirect_url) | |
p = input('Write your PIN:') | |
try: | |
auth.get_access_token(p) | |
except tweepy.TweepError: | |
print("Error! PIN not valid") | |
f = open(ACC_TOKEN_SV, 'w') | |
f.write(auth.access_token+"\n") | |
f.write(auth.access_token_secret) | |
f.close() | |
f = open(ACC_TOKEN_SV, 'r') | |
key = f.readline().strip() | |
secret = f.readline().strip() | |
f.close() | |
auth.set_access_token(key, secret) | |
return auth |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment