This project has been archived by its maintainers, and is no longer receiving any updates.
[!WARNING] This project has not been actively maintained for quite a while. Please consider forking or using an alternative.
FastAPI Discord (OAuth)
Support for "Login with Discord"/ Discord OAuth for FastAPI.
Install
PIP Package fastapi-discord
Example
You can find the Example in examples/
from typing import List
from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI
from fastapi.responses import JSONResponse
from fastapi_discord import DiscordOAuthClient, RateLimited, Unauthorized, User
from fastapi_discord.exceptions import ClientSessionNotInitialized
from fastapi_discord.models import GuildPreview
discord = DiscordOAuthClient(
"<client-id>", "<client-secret>", "<redirect-url>", ("identify", "guilds", "email")
) # scopes
# startup is now deprecated https://fastapi.tiangolo.com/advanced/events/#alternative-events-deprecated
# use lifespan https://fastapi.tiangolo.com/advanced/events/
@asynccontextmanager
async def lifespan(_app: FastAPI):
await discord.init()
yield
app = FastAPI()
# @app.on_event("startup")
# async def on_startup():
# await discord.init()
@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(
"/authenticated",
dependencies=[Depends(discord.requires_authorization)],
response_model=bool,
)
async def isAuthenticated(token: str = Depends(discord.get_token)):
try:
auth = await discord.isAuthenticated(token)
return auth
except Unauthorized:
return False
@app.exception_handler(Unauthorized)
async def unauthorized_error_handler(_, __):
return JSONResponse({"error": "Unauthorized"}, status_code=401)
@app.exception_handler(RateLimited)
async def rate_limit_error_handler(_, e: RateLimited):
return JSONResponse(
{"error": "RateLimited", "retry": e.retry_after, "message": e.message},
status_code=429,
)
@app.exception_handler(ClientSessionNotInitialized)
async def client_session_error_handler(_, e: ClientSessionNotInitialized):
print(e)
return JSONResponse({"error": "Internal Error"}, status_code=500)
@app.get("/user", dependencies=[Depends(discord.requires_authorization)], response_model=User)
async def get_user(user: User = Depends(discord.user)):
return user
@app.get(
"/guilds",
dependencies=[Depends(discord.requires_authorization)],
response_model=List[GuildPreview],
)
async def get_guilds(guilds: List = Depends(discord.guilds)):
return guilds
Inspired by
Thanks to @jnawk and @nwunderly
Metadata
Release files for fastapi-discord 0.2.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_discord-0.2.8-py3-none-any.whl | Python 3 | none | any | Details for fastapi_discord-0.2.8-py3-none-any.whl |
Release files / fastapi_discord-0.2.8-py3-none-any.whl
| Download URL | fastapi_discord-0.2.8-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f10e726a49e73fee12679e7c565cdce4feb453e457d274f7175cd56d41f8bc39
|
|
BLAKE2b-256 checksum How to use checksums |
269eaec225b77f0e3eab0b75d2f815ab0e573acdb331fef74c393f7348e9bcb0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for fastapi-discord 0.2.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.