-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreddit_refresh_token.py
More file actions
78 lines (62 loc) · 2.39 KB
/
Copy pathreddit_refresh_token.py
File metadata and controls
78 lines (62 loc) · 2.39 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import random
import socket
import sys
import praw
# in the library's doc they help with the process of acquiring a refresh token
# but first you need to register your program in reddit itself
# they provide this program to generate it. the program prompts
# for clientID, client_secret and the scopes of the api to be used
# then it open a web page to ask for a permission to access your account
def receive_connection():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("localhost", 8080))
server.listen(1)
client = server.accept()[0]
server.close()
return client
def send_message(client, message):
client.send("HTTP/1.1 200 OK\r\n\r\n{}".format(message).encode("utf-8"))
client.close()
def main():
client_id = input(
"Enter the client ID, it's the line just under Personal use script at the top: "
)
client_secret = input("Enter the client secret, it's the line next to secret: ")
comma_scopes = input(
"Now enter a comma separated list of scopes, or all for all tokens: "
)
if comma_scopes.lower() == "all":
scopes = ["*"]
else:
scopes = comma_scopes.strip().split(",")
reddit = praw.Reddit(
client_id=client_id.strip(),
client_secret=client_secret.strip(),
redirect_uri="http://localhost:8080",
user_agent="praw_refresh_token_example",
)
state = str(random.randint(0, 65000))
url = reddit.auth.url(scopes, state, "permanent")
print("Now open this url in your browser: " + url)
sys.stdout.flush()
client = receive_connection()
data = client.recv(1024).decode("utf-8")
param_tokens = data.split(" ", 2)[1].split("?", 1)[1].split("&")
params = {
key: value for (key, value) in [token.split("=") for token in param_tokens]
}
if state != params["state"]:
send_message(
client,
"State mismatch. Expected: {} Received: {}".format(state, params["state"]),
)
return 1
elif "error" in params:
send_message(client, params["error"])
return 1
refresh_token = reddit.auth.authorize(params["code"])
send_message(client, "Refresh token: {}".format(refresh_token))
return 0
if __name__ == "__main__":
sys.exit(main())