tahamajs's picture
download
raw
1.78 kB
import uvicorn
import requests
import os
import json
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
POST_REQ_URL = "https://github.com/login/oauth/access_token"
GET_REQ_URL = "https://api.github.com/user"
GITHUB_REDIRECT_URI = "http://localhost:8589/oauth/redirect"
GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID")
GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET")
app = FastAPI()
@app.get("/", response_class=HTMLResponse)
def home():
with open("login.html", "r", encoding="utf-8") as f:
return f.read()
def send_post_request(code: str):
response = requests.post(
POST_REQ_URL,
data={
"client_id": GITHUB_CLIENT_ID,
"client_secret": GITHUB_CLIENT_SECRET,
"code": code,
"redirect_uri": GITHUB_REDIRECT_URI
},
headers={"Accept": "application/json"}
)
response_json = response.json()
access_token = response_json.get("access_token")
return access_token
def send_get_request(access_token: str):
response = requests.get(
GET_REQ_URL,
headers={
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github+json"
}
)
response_json = response.json()
return response_json
def convert_to_html_string (user_info: dict):
html_string = f"<h1>Welcome {user_info.get('name')}</h1>"
for key, value in user_info.items():
html_string += f"<p><strong>{key}:</strong> {value}</p>"
return html_string
@app.get("/oauth/redirect")
def oauth_redirect(code: str):
access_token = send_post_request(code)
user_info = send_get_request(access_token)
html_string = convert_to_html_string(user_info)
return HTMLResponse(content=html_string)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8589)

Xet Storage Details

Size:
1.78 kB
·
Xet hash:
2a72132d15fd094ac5c7a72646b6747219d28c9af1424521d83089ee20483b22

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.