MightyOctopus commited on
Commit
a6f3e4f
·
verified ·
1 Parent(s): 41fcb67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -11,8 +11,8 @@ from googleapiclient.errors import HttpError
11
  from langchain.schema import Document
12
 
13
 
14
- TOKEN_PATH = os.getenv("GOOGLE_DOCS_TOKEN_JSON", "secrets/token.json")
15
- CREDENTIALS_PATH = os.getenv("GOOGLE_DOCS_CREDENTIALS_JSON", "secrets/credentials.json")
16
 
17
 
18
  SCOPES = [
@@ -33,21 +33,24 @@ creds = None
33
  ### Google Drive Authentication
34
  def auth_google_docs():
35
  global creds
36
- if os.path.exists(TOKEN_PATH):
37
- creds = Credentials.from_authorized_user_file(TOKEN_PATH, SCOPES)
 
 
 
 
38
 
39
  if not creds or not creds.valid:
40
  if creds and creds.expired and creds.refresh_token:
41
  creds.refresh(Request())
42
  else:
43
- flow = InstalledAppFlow.from_client_config(
44
- CREDENTIALS_PATH, SCOPES
45
- )
46
- creds = flow.run_local_server(port=8080, open_browser=False)
47
-
48
- #Save the credentials
49
- with open(TOKEN_PATH, "w") as token:
50
- token.write(creds.to_json())
51
 
52
  return creds
53
 
 
11
  from langchain.schema import Document
12
 
13
 
14
+ DOCS_TOKEN = os.getenv("GOOGLE_DOCS_TOKEN_JSON")
15
+ DOCS_CREDENTIALS = os.getenv("GOOGLE_DOCS_CREDENTIALS_JSON")
16
 
17
 
18
  SCOPES = [
 
33
  ### Google Drive Authentication
34
  def auth_google_docs():
35
  global creds
36
+ if DOCS_TOKEN:
37
+ try:
38
+ token_json = json.loads(DOCS_TOKEN)
39
+ creds = Credentials.from_authorized_user_file(token_json, SCOPES)
40
+ except Exception:
41
+ creds = None
42
 
43
  if not creds or not creds.valid:
44
  if creds and creds.expired and creds.refresh_token:
45
  creds.refresh(Request())
46
  else:
47
+ if DOCS_CREDENTIALS:
48
+ client_config_json = json.loads(DOCS_CREDENTIALS)
49
+ flow = InstalledAppFlow.from_client_config(
50
+ client_config_json, SCOPES
51
+ )
52
+
53
+ creds = flow.run_local_server(port=8080, open_browser=False)
 
54
 
55
  return creds
56