-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_code_and_token.py
88 lines (49 loc) · 1.75 KB
/
get_code_and_token.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from urllib.parse import urlencode
import webbrowser
import time
import datetime
timestamp = datetime.datetime.now().timestamp()
print("Don't have any 'localhost' running in order to get the 'code' param value that will be generated\n\n")
print("The browser will open and get the value of 'code' from the url bar")
client_id = input('CLIENT ID: ')
redirect_url = "http://localhost"
#scope = "https://www.googleapis.com/auth/blogger"
scope = input('SCOPE[eg:calendar, blogger, email]: ')
scope = f"https://www.googleapis.com/auth/{scope}"
query_params = {
"response_type" : "code",
"client_id" : client_id,
"redirect_uri" : redirect_url,
"scope" : scope
}
url = f"https://accounts.google.com/o/oauth2/auth?{urlencode(query_params)}"
print('Get Ready... Browser will open, COPY the code value quickly')
time.sleep(5)
webbrowser.open(url)
print('Open')
time.sleep(3)
import requests
token_endpoint = "https://oauth2.googleapis.com/token"
time.sleep(5)
code = input('YOUR CODE: ')
client_secret = input("YOUR CLIENT SECRET: ")
data = {
"grant_type" : "authorization_code",
"code" : code,
"redirect_uri" : redirect_url,
"client_id" : client_id,
"client_secret" : client_secret,
"scope" : "https://www.googleapis.com/auth/blogger"
}
response = requests.post(token_endpoint, data=data)
token = response.json()["access_token"]
#create a new text file and add 'token' in it
filename = "NEW_TOKEN_"+str(timestamp)+".txt"
with open(filename, "w") as file:
file.write(token)
print("\n\n\n")
print("New file created: ", filename)
print("\n\n\n")
print("--------------------TOKEN-----------------\n")
print(token, "\n")
print("--------------------------------------------")