| import os | |
| import uvicorn | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import HTMLResponse, RedirectResponse | |
| import requests | |
| app = FastAPI() | |
| # === CONFIGURATION === | |
| # Replace with your real credentials, or set environment variables | |
| CLIENT_ID = os.getenv("GITHUB_CLIENT_ID", "your_client_id_here") | |
| CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET", "your_client_secret_here") | |
| REDIRECT_URI = "http://localhost:8589/oauth/redirect" | |
| GITHUB_AUTH_URL = "https://github.com/login/oauth/authorize" | |
| GITHUB_TOKEN_URL = "https://github.com/login/oauth/access_token" | |
| GITHUB_USER_API = "https://api.github.com/user" | |
| # ====================== | |
| async def login(): | |
| """Redirect the user to GitHub's authorization page.""" | |
| params = { | |
| "client_id": CLIENT_ID, | |
| "redirect_uri": REDIRECT_URI, | |
| "scope": "read:user user:email", # minimal scope | |
| "state": "random_state_string_123" # CSRF protection (hardcoded for demo) | |
| } | |
| auth_url = f"{GITHUB_AUTH_URL}?{requests.compat.urlencode(params)}" | |
| return RedirectResponse(auth_url) | |
| async def oauth_redirect(request: Request): | |
| """ | |
| Callback endpoint. | |
| Receives the authorization code, exchanges it for an access token, | |
| and fetches the user's GitHub profile. | |
| """ | |
| code = request.query_params.get("code") | |
| if not code: | |
| return HTMLResponse("<h3>Error: No authorization code received.</h3>", status_code=400) | |
| # Exchange the authorization code for an access token | |
| token_response = requests.post( | |
| GITHUB_TOKEN_URL, | |
| data={ | |
| "client_id": CLIENT_ID, | |
| "client_secret": CLIENT_SECRET, | |
| "code": code, | |
| "redirect_uri": REDIRECT_URI | |
| }, | |
| headers={"Accept": "application/json"} | |
| ) | |
| token_json = token_response.json() | |
| access_token = token_json.get("access_token") | |
| if not access_token: | |
| return HTMLResponse(f"<h3>Failed to get access token: {token_json}</h3>", status_code=400) | |
| # Use the token to fetch the user's profile | |
| user_response = requests.get( | |
| GITHUB_USER_API, | |
| headers={ | |
| "Authorization": f"token {access_token}", | |
| "Accept": "application/json" | |
| } | |
| ) | |
| user_data = user_response.json() | |
| # Display the information | |
| html = f""" | |
| <h2>Welcome, {user_data.get('name') or user_data.get('login')}!</h2> | |
| <img src="{user_data.get('avatar_url')}" width="100" /> | |
| <ul> | |
| <li><b>Username:</b> {user_data.get('login')}</li> | |
| <li><b>Name:</b> {user_data.get('name', 'N/A')}</li> | |
| <li><b>Email:</b> {user_data.get('email', 'N/A')}</li> | |
| <li><b>Public Repos:</b> {user_data.get('public_repos')}</li> | |
| </ul> | |
| """ | |
| return HTMLResponse(html) | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="localhost", port=8589) |
Xet Storage Details
- Size:
- 2.89 kB
- Xet hash:
- 7a4142c4dae0ae4e88c386cfc80010975b11a87f4f9471e55f14f05774edaaaa
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.