Discord OAuth FastAPI extension for APIs
Project description
FastAPI Discord (OAuth)
Support for "Login with Discord"/ Discord OAuth for FastAPI.
Install
PIP Package fastapi-discord
Example
You can find the Example in expamples/
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi_discord import DiscordOAuthClient, Unauthorized, RateLimited
import uvicorn
app = FastAPI()
discord = DiscordOAuthClient("<client-id>", "<client-secret>", "<redirect-url>",
("identify", "guilds", "email")) # scopes
@app.get("/login")
async def login():
return {
"url": discord.oauth_login_url
}
@app.get("/callback")
async def callback(code: str):
token, refresh_token = await discord.get_access_token(code)
return {
"access_token": token,
"refresh_token": refresh_token
}
@app.get("/hello")
@discord.requires_authorization
async def hello(request: Request):
return await discord.user(request)
return f'Hello {user.username}#{user.discriminator}!'
@app.get('/authenticated')
async def auth(request: Request):
try:
token = discord.get_token(request)
auth = await discord.isAuthenticated(token)
return f'{auth}'
except Unauthorized:
return 'False'
@app.get('/refresh')
async def refresh(refresh_token: str):
token, refresh_token = await discord.refresh_access_token(refresh_token)
return {
"access_token": token,
"refresh_token": refresh_token
}
@app.exception_handler(Unauthorized)
async def unauthorized_error_handler(request: Request, e: Unauthorized):
return JSONResponse({
"error": "Unauthorized"
})
@app.exception_handler(RateLimited)
async def ratelimit_error_handler(request: Request, e: RateLimited):
return JSONResponse({
"error": "RateLimited",
"retry": e.retry_after,
"message": e.message
})
@app.get("/require_auth")
@discord.requires_authorization
async def test(request: Request):
return 'Hello!'
uvicorn.run(app, port=5000)
Inspired by
Starlette-Discord Quart-Discord-OAuth Quart-Discord
Thanks to @jnawk and @nwunderly
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for fastapi_discord-0.1.4.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 762794de19d62afab38249a9e200cb7f6f06a5780839b01d1d18cce04b1f42bb |
|
MD5 | 725099c9ea5745c093930be14b215be8 |
|
BLAKE2b-256 | bf7009b7baa32ea04baed1d20e3555cfa79855f7cf751e466af1a8f21df189a3 |