Skip to content

Instantly share code, notes, and snippets.

@byquanton
Created November 25, 2022 15:53
Show Gist options
  • Save byquanton/d10973431fbcc4392e9623e7eb5ad96e to your computer and use it in GitHub Desktop.
Save byquanton/d10973431fbcc4392e9623e7eb5ad96e to your computer and use it in GitHub Desktop.
import requests
API_ENDPOINT = 'https://discord.com/api/v10'
CLIENT_ID = 'client_id'
CLIENT_SECRET = 'client_secret'
REDIRECT_URI = 'https://domain.tld'
def exchange_code(code):
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': REDIRECT_URI
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers)
r.raise_for_status()
return r.json()
def refresh_token(refresh_token):
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers)
r.raise_for_status()
return r.json()
def get_user(access_token):
headers = {
'Authorization': 'Bearer %s' % access_token
}
r = requests.get('%s/users/@me' % API_ENDPOINT, headers=headers)
r.raise_for_status()
return r.json()
def get_guilds(access_token):
headers = {
'Authorization': 'Bearer %s' % access_token
}
r = requests.get('%s/users/@me/guilds' % API_ENDPOINT, headers=headers)
r.raise_for_status()
return r.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment