{"id":23387043,"url":"https://github.com/tert0/fastapi-discord","last_synced_at":"2025-04-04T22:02:21.547Z","repository":{"id":38193028,"uuid":"350680378","full_name":"Tert0/fastapi-discord","owner":"Tert0","description":"Discord OAuth FastAPI extension for APIs","archived":false,"fork":false,"pushed_at":"2025-03-24T13:46:17.000Z","size":221,"stargazers_count":70,"open_issues_count":8,"forks_count":18,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T21:05:54.458Z","etag":null,"topics":["aiohttp","asyncio","fastapi","hacktoberfest","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tert0.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-23T11:06:28.000Z","updated_at":"2025-03-24T13:46:16.000Z","dependencies_parsed_at":"2023-09-22T05:37:23.727Z","dependency_job_id":"d9ba5029-d2a1-4f51-badd-99c12748c162","html_url":"https://github.com/Tert0/fastapi-discord","commit_stats":{"total_commits":93,"total_committers":8,"mean_commits":11.625,"dds":0.5268817204301075,"last_synced_commit":"166fc1dc696771e2abfb4aad998be2905a212acc"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tert0%2Ffastapi-discord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tert0%2Ffastapi-discord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tert0%2Ffastapi-discord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tert0%2Ffastapi-discord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tert0","download_url":"https://codeload.github.com/Tert0/fastapi-discord/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256102,"owners_count":20909240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aiohttp","asyncio","fastapi","hacktoberfest","python"],"created_at":"2024-12-22T01:14:24.002Z","updated_at":"2025-04-04T22:02:21.532Z","avatar_url":"https://github.com/Tert0.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI version](https://badge.fury.io/py/fastapi-discord.svg)](https://badge.fury.io/py/fastapi-discord)\n[![PyPI Downloads](https://img.shields.io/pypi/dm/fastapi-discord.svg)](https://pypi.org/project/fastapi-discord)\n\n# FastAPI Discord (OAuth)\nSupport for \"Login with Discord\"/ Discord OAuth for FastAPI.\n# Install\nPIP Package `fastapi-discord`\n# Example\nYou can find the Example in [examples/](https://github.com/Tert0/fastapi-discord/tree/master/examples)\n```py\nfrom typing import List\nfrom contextlib import asynccontextmanager\nfrom fastapi import Depends, FastAPI\nfrom fastapi.responses import JSONResponse\nfrom fastapi_discord import DiscordOAuthClient, RateLimited, Unauthorized, User\nfrom fastapi_discord.exceptions import ClientSessionNotInitialized\nfrom fastapi_discord.models import GuildPreview\n\n\ndiscord = DiscordOAuthClient(\n    \"\u003cclient-id\u003e\", \"\u003cclient-secret\u003e\", \"\u003credirect-url\u003e\", (\"identify\", \"guilds\", \"email\")\n)  # scopes\n\n# startup is now deprecated https://fastapi.tiangolo.com/advanced/events/#alternative-events-deprecated\n# use lifespan https://fastapi.tiangolo.com/advanced/events/\n\n@asynccontextmanager\nasync def lifespan(_app: FastAPI):\n    await discord.init()\n    yield\n\napp = FastAPI()\n\n# @app.on_event(\"startup\")\n# async def on_startup():\n#     await discord.init()\n\n\n@app.get(\"/login\")\nasync def login():\n    return {\"url\": discord.oauth_login_url}\n\n\n@app.get(\"/callback\")\nasync def callback(code: str):\n    token, refresh_token = await discord.get_access_token(code)\n    return {\"access_token\": token, \"refresh_token\": refresh_token}\n\n\n@app.get(\n    \"/authenticated\",\n    dependencies=[Depends(discord.requires_authorization)],\n    response_model=bool,\n)\nasync def isAuthenticated(token: str = Depends(discord.get_token)):\n    try:\n        auth = await discord.isAuthenticated(token)\n        return auth\n    except Unauthorized:\n        return False\n\n\n@app.exception_handler(Unauthorized)\nasync def unauthorized_error_handler(_, __):\n    return JSONResponse({\"error\": \"Unauthorized\"}, status_code=401)\n\n\n@app.exception_handler(RateLimited)\nasync def rate_limit_error_handler(_, e: RateLimited):\n    return JSONResponse(\n        {\"error\": \"RateLimited\", \"retry\": e.retry_after, \"message\": e.message},\n        status_code=429,\n    )\n\n\n@app.exception_handler(ClientSessionNotInitialized)\nasync def client_session_error_handler(_, e: ClientSessionNotInitialized):\n    print(e)\n    return JSONResponse({\"error\": \"Internal Error\"}, status_code=500)\n\n\n@app.get(\"/user\", dependencies=[Depends(discord.requires_authorization)], response_model=User)\nasync def get_user(user: User = Depends(discord.user)):\n    return user\n\n\n@app.get(\n    \"/guilds\",\n    dependencies=[Depends(discord.requires_authorization)],\n    response_model=List[GuildPreview],\n)\nasync def get_guilds(guilds: List = Depends(discord.guilds)):\n    return guilds\n\n```\n\n# Inspired by\n[Starlette-Discord](https://github.com/nwunderly/starlette-discord)\n\n[Quart-Discord-OAuth](https://github.com/Tert0/Quart-Discord-OAuth/)\n\n[Quart-Discord](https://github.com/jnawk/quart-discord)\n\nThanks to @jnawk and @nwunderly\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftert0%2Ffastapi-discord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftert0%2Ffastapi-discord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftert0%2Ffastapi-discord/lists"}