-
-
Save eitozx/30aa328dbb2a7adc87eb0addd9ecd556 to your computer and use it in GitHub Desktop.
Basic setup that I personally use for discord authentication.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLIENT_ID = | |
AUTH_URL = | |
CLIENT_SECRET = | |
REDIRECT_URI = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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