-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthentication.py
More file actions
58 lines (57 loc) · 1.83 KB
/
Copy pathauthentication.py
File metadata and controls
58 lines (57 loc) · 1.83 KB
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
from config import getAccounts, setAccounts
from time import time
import fileinput
# Returns the new r variable on success, None on failure
def login(r, username):
accounts = getAccounts()
if accounts == None:
accounts = {}
access_info = None
if username not in accounts:
import webbrowser
# Currently this is all scopes
# TODO: Narrow this list down to only what we need for the bot
scope = [
'creddits',
'edit',
'flair',
'history',
'identity',
'modconfig',
'modcontributors',
'modflair',
'modlog',
'modothers',
'modposts',
'modself',
'modwiki',
'mysubreddits',
'privatemessages',
'read',
'report',
'save',
'submit',
'subscribe',
'vote',
'wikiedit',
'wikiread'
]
url = r.get_authorize_url('', ' '.join(scope), True)
webbrowser.open(url)
access_code = raw_input("Click \"Accept\" and enter the `code` parameter from the URL: ")
access_info = r.get_access_information(access_code)
elif accounts[username]['expires'] < time():
access_info = r.refresh_access_information(accounts[username]['refresh_token'])
else:
access_info = r.set_access_credentials(set(accounts[username]['scope'].split()),
accounts[username]['access_token'],
accounts[username]['refresh_token'])
return r
accounts[username] = {
'access_token': access_info['access_token'],
'refresh_token': access_info['refresh_token'],
'scope': ' '.join(access_info['scope']),
'expires': int(time()) + 60 * 60
}
setAccounts(accounts)
return r