zinoubm commited on
Commit
7fc3da4
·
1 Parent(s): e37e95e

changing redirect url's

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import RedirectResponse, HTMLResponse
 
3
  from fastapi.staticfiles import StaticFiles
4
  from google_manager.constants import SCOPES
5
  from pages.load_page import load_page
@@ -24,9 +25,18 @@ async def home(request: Request):
24
  html_content = load_page("./pages/auth_page.html")
25
  # print("request session")
26
  # print(request.session)
27
- creds = request.session.get("credentials", None)
28
- # print("state")
29
- # print(creds)
 
 
 
 
 
 
 
 
 
30
  if not creds or not creds.valid:
31
  return HTMLResponse(content=html_content, status_code=200)
32
  else:
@@ -40,7 +50,15 @@ async def integrate_google(request: Request):
40
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
41
  "credentials.json", scopes=SCOPES
42
  )
43
- flow.redirect_uri = "https://gpt-summary-u8pr.onrender.com/auth_callback"
 
 
 
 
 
 
 
 
44
  authorization_url, state = flow.authorization_url(
45
  access_type="offline",
46
  include_granted_scopes="true",
@@ -56,7 +74,26 @@ async def auth_callback(request: Request):
56
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
57
  "credentials.json", scopes=SCOPES, state=state
58
  )
59
- flow.redirect_uri = "https://gpt-summary-u8pr.onrender.com/auth_callback"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  # Use the authorization server's response to fetch the OAuth 2.0 tokens.
62
  authorization_response = str(request.url)
 
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import RedirectResponse, HTMLResponse
3
+ from google.oauth2.credentials import Credentials
4
  from fastapi.staticfiles import StaticFiles
5
  from google_manager.constants import SCOPES
6
  from pages.load_page import load_page
 
25
  html_content = load_page("./pages/auth_page.html")
26
  # print("request session")
27
  # print(request.session)
28
+ # creds = request.session.get("credentials", None)
29
+ creds = request.session.get("credentials", {})
30
+
31
+ if creds:
32
+ try:
33
+ creds = Credentials.from_authorized_user_info(info=creds)
34
+
35
+ except Exception as e:
36
+ raise Exception(
37
+ f"Invalid credentials in session with the following error{e}"
38
+ )
39
+
40
  if not creds or not creds.valid:
41
  return HTMLResponse(content=html_content, status_code=200)
42
  else:
 
50
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
51
  "credentials.json", scopes=SCOPES
52
  )
53
+ # flow.redirect_uri = "https://gpt-summary-u8pr.onrender.com/auth_callback"
54
+ # flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
55
+ flow.redirect_uri = (
56
+ request.url.scheme
57
+ + "://"
58
+ + request.url.hostname
59
+ + (":" + str(request.url.port) if request.url.port else "")
60
+ + app.url_path_for("auth_callback")
61
+ )
62
  authorization_url, state = flow.authorization_url(
63
  access_type="offline",
64
  include_granted_scopes="true",
 
74
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
75
  "credentials.json", scopes=SCOPES, state=state
76
  )
77
+ print(request.url.hostname)
78
+ # if request.url.hostname == "0.0.0.0":
79
+ # flow.redirect_uri = "https://gpt-summary-u8pr.onrender.com/auth_callback"
80
+ # else:
81
+ # flow.redirect_uri = (
82
+ # request.url.scheme
83
+ # + "://"
84
+ # + request.url.hostname
85
+ # + (":" + str(request.url.port) if request.url.port else "")
86
+ # + app.url_path_for("auth_callback")
87
+ # )
88
+ # flow.redirect_uri = "https://gpt-summary-u8pr.onrender.com/auth_callback"
89
+ # flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
90
+ flow.redirect_uri = (
91
+ request.url.scheme
92
+ + "://"
93
+ + request.url.hostname
94
+ + (":" + str(request.url.port) if request.url.port else "")
95
+ + app.url_path_for("auth_callback")
96
+ )
97
 
98
  # Use the authorization server's response to fetch the OAuth 2.0 tokens.
99
  authorization_response = str(request.url)