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 typing import List
from fastapi import Depends, FastAPI
from fastapi.responses import JSONResponse
from fastapi_discord import DiscordOAuthClient, RateLimited, Unauthorized, User
from fastapi_discord.models import GuildPreview
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(
"/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.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
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.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2efbac00681d7c369c9cb305d0e28b2cc78be8202355d5e87281d748a9629a3c |
|
MD5 | c3bfbbb00f40bc5899b3269c19683793 |
|
BLAKE2b-256 | 6a5b7f1234b0c00f94c6e23fb555f9bb8ef5f1b6e6eea85d52b272f37de2ce03 |