-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.py
64 lines (52 loc) · 2 KB
/
auth.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
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
'''
author: Daniele Faugiana
last update: October the 20th 2012
version: 0.1
'''
import os
import tweepy
import webbrowser
from consumer import CONSUMER_KEY, CONSUMER_SECRET
from access import ACCESS_KEY, ACCESS_SECRET
def startAuth():
#Firstly check if CK and CS are available
if ((not CONSUMER_KEY) or (not CONSUMER_SECRET)):
print 'CCK ERROR, KEYS ARE NOT AVAILABLE ANYMORE'
print 'Please, register a new app on Twitter Developers'
#If available, it starts a new OAuth handler
auth = tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
#Now it checks if accesses are available
if (ACCESS_KEY and ACCESS_SECRET):
#If available start an api instance
auth.set_access_token(ACCESS_KEY,ACCESS_SECRET)
api = tweepy.API(auth)
return api
else:
#Else it start the authentication procedure
auth_url = auth.get_authorization_url()
webbrowser.open(auth_url)
print 'Write here the pin after browser authentication'
verifier = raw_input(' Insert PIN: ').strip()
auth.get_access_token(verifier)
accessKey = auth.access_token.key
accessSecret = auth.access_token.secret
#It gets Access keys and show them
ak = 'ACCESS_KEY = "' + str(accessKey) + '"\n'
aS = 'ACCESS_SECRET = "' + str(accessSecret) + '"\n'
print 'These are your personal keys'
print ak
print aS
#It overwrites keys in the access file
thispath = os.path.abspath(__file__)
thispath = os.path.dirname(thispath)
filePath = thispath + '/access.py'
accessfile = open(filePath,'w')
accessfile.write(ak)
accessfile.write(aS)
accessfile.close()
print 'Your keys have been saved in', filePath, '\n \n'
#Finally it calls itself to get the api object
auth.set_access_token(accessKey,accessSecret)
api = tweepy.API(auth)
return api