This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
credentials.py.example
73 lines (63 loc) · 2.45 KB
/
credentials.py.example
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
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""Please read this before connecting to Reddit:
https://github.com/reddit/reddit/wiki/API
http://praw.readthedocs.io/en/latest/getting_started/authentication.html
"""
# bot name
username = 'yourbotname'
# bot sends messages to admin if refresh fails
# bot forwards PMs to admin if they don't contain cards
admin_username = 'yourusername'
# subreddits to visit
subreddits = ["hearthstone"]
# user to skip while scanning
userBlacklist = ['MTGCardFetcher']
# enter this url in reddit https://www.reddit.com/prefs/apps/
# don't modify: the same ip and port are used below from local server
redirect_uri = 'http://127.0.0.1:65010/authorize_callback'
"""to get a refresh_token:
- create an app at reddit https://www.reddit.com/prefs/apps/
- create a praw.ini:
[bot]
client_id=clientIdFromPrefsApps
client_secret=clientSecretFromPrefsApps
user_agent=praw:yourbotname:1.0 (by /u/yourusername)
- python credentials.py
- copy refreshToken and update praw.ini
"""
if __name__ == "__main__":
import sys
import http.server
from urllib.parse import urlparse
import praw
import time
# see below, starts webserver and browser
redirect_is_local = True
r = praw.Reddit('bot', redirect_uri=redirect_uri)
oauthState = str(int(time.time()))
url = r.auth.url(('submit', 'privatemessages', 'read', 'identity'),
oauthState, 'permanent')
print("copy, call url and confirm, copy code from redirected url")
print(url)
if redirect_is_local:
import webbrowser
webbrowser.open(url)
# start local http server for callback, prints refreshToken
if redirect_is_local:
class CallbackHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
query = urlparse(self.path).query
queryParts = dict(qc.split("=") for qc in query.split("&"))
if oauthState != queryParts['state']:
print('wrong state')
self.wfile.write(bytes('wrong state', "utf8"))
return
code = queryParts['code']
refreshToken = r.auth.authorize(code)
text = "authCode:" + code + '\nrefreshToken:' + refreshToken
print(text)
self.wfile.write(bytes(text, "utf8"))
server = http.server.HTTPServer(("127.0.0.1", 65010), CallbackHandler)
server.timeout = None
server.handle_request()
server.server_close()