Skip to content

Instantly share code, notes, and snippets.

@eitozx
Created October 25, 2022 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eitozx/30aa328dbb2a7adc87eb0addd9ecd556 to your computer and use it in GitHub Desktop.
Save eitozx/30aa328dbb2a7adc87eb0addd9ecd556 to your computer and use it in GitHub Desktop.
Basic setup that I personally use for discord authentication.
CLIENT_ID =
AUTH_URL =
CLIENT_SECRET =
REDIRECT_URI =
import os
import dotenv
dotenv.load_dotenv()
CLIENT_ID = int(os.getenv("CLIENT_ID"))
AUTH_URL = os.getenv("AUTH_URL")
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
REDIRECT_URI = os.getenv("REDIRECT_URI")
API_URL = "https://discord.com/api/v10/"
import const
import requests
def exchange_code(code : str):
"""
where `code` is the code that you get after logging your discord account via `AUTH_URL`
"""
data = {
"client_id" : const.CLIENT_ID,
"client_secret" : const.CLIENT_SECRET,
"grant_type" : "authorization_code",
"code" : code,
"redirect_uri" : const.REDIRECT_URI,
"scope" : "identify email guilds guilds.join"
}
headers = {
"Conent-Type" : "application/x-www-form-urlencoded"
}
credentials = requests.post(
url = "https://discord.com/api/oauth2/token",
data = data,
headers=headers
).json()
access_token = credentials["access_token"]
# print(access_token)
return requests.get(
url = f"{const.API_URL}users/@me",
headers={'Authorization' : f'Bearer {access_token}'}
).json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment