-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwauth.py
53 lines (42 loc) · 1.66 KB
/
twauth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import unicode_literals
import csv
import os
import webbrowser
import tweepy as tp
from setup import FileImport
class OAuthorizer():
def __init__(self):
ctoken, csecret = FileImport().read_app_key_file()
auth = tp.OAuthHandler(ctoken, csecret)
try:
redirect_url = auth.get_authorization_url()
except tp.TweepError as e:
if '"code":32' in e.reason:
raise tp.TweepError("""Failed to get the request token. Perhaps the Consumer Key
and / or secret in your 'keys.json' is incorrect?""")
else:
raise e
webbrowser.open(redirect_url)
token = auth.request_token["oauth_token"]
verifier = input("Please enter Verifier Code: ")
auth.request_token = {'oauth_token': token,
'oauth_token_secret': verifier}
try:
auth.get_access_token(verifier)
except tp.TweepError as e:
if "Invalid oauth_verifier parameter" in e.reason:
raise tp.TweepError("""Failed to get access token! Perhaps the
verifier you've entered is wrong.""")
else:
raise e
if not os.path.isfile('tokens.csv'):
with open('tokens.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(["token", "secret"])
f.close()
with open('tokens.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow([auth.access_token, auth.access_token_secret])
f.close()
if __name__ == "__main__":
OAuthorizer()