import uvicorn from fastapi import FastAPI, HTTPException, Request, Depends, BackgroundTasks from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.templating import Jinja2Templates from fastapi.security import OAuth2AuthorizationCodeBearer from google.oauth2 import id_token from google.auth.transport import requests as grequests app = FastAPI(debug=True) templates = Jinja2Templates(directory="templates") CLIENT_ID = "931024815427-72meiq3uivuuolfukb3jvikvhhqlr574.apps.googleusercontent.com" CLIENT_SECRET = "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" REDIRECT_URI = "http://localhost:3000/auth/google/callback" oauth2_scheme = OAuth2AuthorizationCodeBearer( authorizationUrl="https://accounts.google.com/o/oauth2/auth", tokenUrl="https://oauth2.googleapis.com/token", ) @app.get("/") async def index(request: Request): return templates.TemplateResponse("index.html", {"request": request}) @app.get("/auth/google") async def auth_google(request: Request): google_auth_url = ( f"https://accounts.google.com/o/oauth2/auth" f"?client_id={CLIENT_ID}" f"&redirect_uri={REDIRECT_URI}" f"&response_type=code" f"&scope=email%20profile%20openid" ) return RedirectResponse(url=google_auth_url) @app.get("/auth/google/callback") async def auth_google_callback(code: str): token_request_data = { "code": code, "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET, "redirect_uri": REDIRECT_URI, "grant_type": "authorization_code" } token_response = grequests.post("https://oauth2.googleapis.com/token", data=token_request_data) access_token = token_response.json().get("access_token") # Store access_token in session or database for future use return {"access_token": access_token} @app.get("/fetch_comments") async def fetch_comments(access_token: str = Depends(oauth2_scheme)): # Fetch comments from Google Docs API using access_token document_id = "your_document_id" headers = {"Authorization": f"Bearer {access_token}"} response = grequests.get(f"https://docs.googleapis.com/v1/documents/{document_id}", headers=headers) if response.status_code == 200: comments = response.json().get("comments", []) return comments else: raise HTTPException(status_code=response.status_code, detail="Failed to fetch comments") if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=3000)