-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord_utils.py
36 lines (32 loc) · 1.13 KB
/
discord_utils.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
from flask import redirect, request, session
import requests
import os
CLIENT_ID = os.getenv('DISCORD_CLIENT_ID')
CLIENT_SECRET = os.getenv('DISCORD_CLIENT_SECRET')
REDIRECT_URI = os.getenv('DISCORD_REDIRECT_URI')
API_BASE_URL = "https://discord.com/api"
AUTHORIZATION_BASE_URL = API_BASE_URL + "/oauth2/authorize"
USER_URL = API_BASE_URL + "/users/@me"
TOKEN_URL = API_BASE_URL + "/oauth2/token"
def get_discord_login_url():
return f"{AUTHORIZATION_BASE_URL}?response_type=code&client_id={CLIENT_ID}&scope=identify&redirect_uri={REDIRECT_URI}&prompt=consent"
def get_token(code):
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI,
'scope': 'identify',
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.post(TOKEN_URL, data=data, headers=headers)
return response.json()
def get_user_info(token):
headers = {
'Authorization': f"Bearer {token}"
}
response = requests.get(USER_URL, headers=headers)
return response.json()