Rahulk2197 commited on
Commit
bf24c39
·
verified ·
1 Parent(s): 85964d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -1,11 +1,11 @@
1
- import os
2
- import pickle
3
  from google_auth_oauthlib.flow import InstalledAppFlow
4
  from google.auth.transport.requests import Request
5
- import streamlit as st
6
- import hashlib
7
  from googleapiclient.discovery import build
8
  from googleapiclient.errors import HttpError
 
 
 
9
 
10
  # Constants
11
  CREDS_FILE = 'token.pickle'
@@ -65,18 +65,26 @@ def get_credentials():
65
  return creds
66
 
67
  flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_FILE, SCOPES)
68
- auth_url, _ = flow.authorization_url(prompt='consent') # Generate authorization URL
69
- st.write(f"Please go to this URL to authorize: [Authorize URL]({auth_url})")
70
 
71
- # Prompt the user to input the authorization code
72
- code = st.text_input("Enter the authorization code")
73
-
74
- if code:
75
- creds = flow.fetch_token(code=code) # Exchange authorization code for tokens
 
 
 
 
 
 
 
 
 
 
 
76
  with open(CREDS_FILE, 'wb') as token:
77
  pickle.dump(creds, token)
78
  return creds
79
- return None
80
 
81
  # Sign Up Page
82
  def signup(service):
 
1
+ import streamlit as st
 
2
  from google_auth_oauthlib.flow import InstalledAppFlow
3
  from google.auth.transport.requests import Request
 
 
4
  from googleapiclient.discovery import build
5
  from googleapiclient.errors import HttpError
6
+ import pickle
7
+ import os
8
+ import hashlib
9
 
10
  # Constants
11
  CREDS_FILE = 'token.pickle'
 
65
  return creds
66
 
67
  flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_FILE, SCOPES)
 
 
68
 
69
+ # If running locally or if no browser is available
70
+ if os.environ.get('STREAMLIT_RUN'):
71
+ # Manual code entry for desktop apps
72
+ auth_url, _ = flow.authorization_url(prompt='consent')
73
+ st.write(f"Please visit this URL to authorize: [Authorize URL]({auth_url})")
74
+
75
+ code = st.text_input("Enter the authorization code")
76
+
77
+ if code:
78
+ creds = flow.fetch_token(code=code)
79
+ with open(CREDS_FILE, 'wb') as token:
80
+ pickle.dump(creds, token)
81
+ return creds
82
+ else:
83
+ # Use local server flow if browser is available
84
+ creds = flow.run_local_server(port=0)
85
  with open(CREDS_FILE, 'wb') as token:
86
  pickle.dump(creds, token)
87
  return creds
 
88
 
89
  # Sign Up Page
90
  def signup(service):