Skip to main content

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


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

If you're not sure about the file name format, learn more about wheel file names.

fastapi_discord-0.1.4.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_discord-0.1.4.2-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for fastapi_discord-0.1.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ca2fd8599b6e65ebdd4ee93a1b743ece746b5957c8d8bfbd2b25b7f1b9d35057
MD5 33d0b4f585ade3f92de0ceccc80204c7
BLAKE2b-256 cb9abc8451e65cfa2c18561bdecf50b102e370cd6cd192cf65b5ec0e2c0906e6

See more details on using hashes here.

Supported by

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