Skip to main content

Discord OAuth FastAPI extension for APIs

Project description

PyPI version PyPI Downloads

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 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

app = FastAPI()

discord = DiscordOAuthClient(
    "<client-id>", "<client-secret>", "<redirect-url>", ("identify", "guilds", "email")
)  # scopes


@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

Starlette-Discord

Quart-Discord-OAuth

Quart-Discord

Thanks to @jnawk and @nwunderly

Project details


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

fastapi_discord-0.2.7-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_discord-0.2.7-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_discord-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 92fa5d12cc60b0eba8fbbd7d52d987f1a3fd088f6057ada9c6a4f93eb0f079b4
MD5 670acd6303e4448bd6731601c5c98fc7
BLAKE2b-256 d8d768b9a5cacc1002037503c5ffaf1a3b34b2f0c4803d80e2aafdf4ec2412a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page