Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,64 @@
|
|
1 |
-
import
|
2 |
-
from
|
|
|
|
|
|
|
3 |
from google.oauth2 import id_token
|
4 |
from google.auth.transport import requests as grequests
|
5 |
|
6 |
-
app =
|
7 |
-
|
8 |
|
9 |
CLIENT_ID = "your_google_client_id"
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
@app.
|
17 |
-
def index():
|
18 |
-
return "
|
19 |
|
20 |
-
@app.
|
21 |
-
def
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
-
@app.
|
29 |
-
def
|
30 |
-
# Exchange authorization code for access token
|
31 |
-
code = request.args.get("code")
|
32 |
token_request_data = {
|
33 |
"code": code,
|
34 |
"client_id": CLIENT_ID,
|
35 |
-
"client_secret":
|
36 |
-
"redirect_uri":
|
37 |
"grant_type": "authorization_code"
|
38 |
}
|
39 |
-
token_response =
|
40 |
access_token = token_response.json().get("access_token")
|
41 |
-
|
42 |
-
|
43 |
-
session["access_token"] = access_token
|
44 |
-
return redirect(url_for("fetch_comments"))
|
45 |
-
else:
|
46 |
-
return "Failed to obtain access token."
|
47 |
|
48 |
-
@app.
|
49 |
-
def fetch_comments():
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
comments = response.json().get("comments", [])
|
59 |
-
return str(comments)
|
60 |
-
else:
|
61 |
-
return f"Failed to fetch comments. Status code: {response.status_code}"
|
62 |
else:
|
63 |
-
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
-
|
67 |
-
|
|
|
1 |
+
import uvicorn
|
2 |
+
from fastapi import FastAPI, HTTPException, Request, Depends, BackgroundTasks
|
3 |
+
from fastapi.responses import HTMLResponse, RedirectResponse
|
4 |
+
from fastapi.templating import Jinja2Templates
|
5 |
+
from fastapi.security import OAuth2AuthorizationCodeBearer, OAuth2AuthorizeRequest
|
6 |
from google.oauth2 import id_token
|
7 |
from google.auth.transport import requests as grequests
|
8 |
|
9 |
+
app = FastAPI(debug=True)
|
10 |
+
templates = Jinja2Templates(directory="templates")
|
11 |
|
12 |
CLIENT_ID = "your_google_client_id"
|
13 |
+
CLIENT_SECRET = "your_google_client_secret"
|
14 |
+
REDIRECT_URI = "http://localhost:7860/auth/google/callback"
|
15 |
|
16 |
+
oauth2_scheme = OAuth2AuthorizationCodeBearer(
|
17 |
+
authorizationUrl="https://accounts.google.com/o/oauth2/auth",
|
18 |
+
tokenUrl="https://oauth2.googleapis.com/token",
|
19 |
+
)
|
20 |
|
21 |
+
@app.get("/")
|
22 |
+
async def index(request: Request):
|
23 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
24 |
|
25 |
+
@app.get("/auth/google")
|
26 |
+
async def auth_google(request: Request):
|
27 |
+
google_auth_url = (
|
28 |
+
f"https://accounts.google.com/o/oauth2/auth"
|
29 |
+
f"?client_id={CLIENT_ID}"
|
30 |
+
f"&redirect_uri={REDIRECT_URI}"
|
31 |
+
f"&response_type=code"
|
32 |
+
f"&scope=email%20profile%20openid"
|
33 |
+
)
|
34 |
+
return RedirectResponse(url=google_auth_url)
|
35 |
|
36 |
+
@app.get("/auth/google/callback")
|
37 |
+
async def auth_google_callback(code: str):
|
|
|
|
|
38 |
token_request_data = {
|
39 |
"code": code,
|
40 |
"client_id": CLIENT_ID,
|
41 |
+
"client_secret": CLIENT_SECRET,
|
42 |
+
"redirect_uri": REDIRECT_URI,
|
43 |
"grant_type": "authorization_code"
|
44 |
}
|
45 |
+
token_response = grequests.post("https://oauth2.googleapis.com/token", data=token_request_data)
|
46 |
access_token = token_response.json().get("access_token")
|
47 |
+
# Store access_token in session or database for future use
|
48 |
+
return {"access_token": access_token}
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
@app.get("/fetch_comments")
|
51 |
+
async def fetch_comments(access_token: str = Depends(oauth2_scheme)):
|
52 |
+
# Fetch comments from Google Docs API using access_token
|
53 |
+
document_id = "your_document_id"
|
54 |
+
headers = {"Authorization": f"Bearer {access_token}"}
|
55 |
+
response = grequests.get(f"https://docs.googleapis.com/v1/documents/{document_id}", headers=headers)
|
56 |
+
|
57 |
+
if response.status_code == 200:
|
58 |
+
comments = response.json().get("comments", [])
|
59 |
+
return comments
|
|
|
|
|
|
|
|
|
60 |
else:
|
61 |
+
raise HTTPException(status_code=response.status_code, detail="Failed to fetch comments")
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|