zinoubm's picture
pushing for test
027a338
raw
history blame
2.66 kB
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google_manager.constants import SCOPES
from starlette.responses import RedirectResponse
from gradio.routes import App
import google.oauth2.credentials
import google_auth_oauthlib.flow
def authenticate(SCOPES):
"""
Request access for the google docs api
"""
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=7200)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
return creds
def get_oauth_url(SCOPES, redirect_url):
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
"credentials.json", scopes=SCOPES
)
# flow.redirect_uri = "http://127.0.0.1:8000/gradio/?__theme=dark"
# flow.redirect_uri = app.url_path_for(redirect_url)
flow.redirect_uri = redirect_url
# Generate URL for request to Google's OAuth 2.0 server.
# Use kwargs to set optional request parameters.
authorization_url, state = flow.authorization_url(
# Enable offline access so that you can refresh an access token without
# re-prompting the user for permission. Recommended for web server apps.
access_type="offline",
# Enable incremental authorization. Recommended as a best practice.
include_granted_scopes="true",
)
print("state")
print(state)
return authorization_url
def authenticate_production(SCOPES):
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=7200)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
return creds