MightyOctopus commited on
Commit
c12f762
·
verified ·
1 Parent(s): cfa2475

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -4,17 +4,12 @@ import os.path, json
4
  from typing import List
5
 
6
  from google.auth.transport.requests import Request
7
- from google.oauth2.credentials import Credentials
8
- from google_auth_oauthlib.flow import InstalledAppFlow
9
  from googleapiclient.discovery import build
10
  from googleapiclient.errors import HttpError
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 = [
19
  "https://www.googleapis.com/auth/drive.readonly",
20
  "https://www.googleapis.com/auth/documents.readonly",
@@ -30,29 +25,38 @@ LOCAL_SEO_FILEID = "1uc4qH5roh6_xzv5x4osZG7nrPHpPUqRz9qPn31Y1azY"
30
 
31
  creds = None
32
 
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.run_local_server(
50
- client_config_json, SCOPES, port=0
51
- )
 
 
 
 
 
 
 
 
 
52
 
53
- creds = flow.run_console()
54
 
55
- return creds
56
 
57
 
58
  def get_docs_text() -> List[Document]:
 
4
  from typing import List
5
 
6
  from google.auth.transport.requests import Request
7
+ from google.oauth2 import service_account
 
8
  from googleapiclient.discovery import build
9
  from googleapiclient.errors import HttpError
10
  from langchain.schema import Document
11
 
12
 
 
 
 
 
13
  SCOPES = [
14
  "https://www.googleapis.com/auth/drive.readonly",
15
  "https://www.googleapis.com/auth/documents.readonly",
 
25
 
26
  creds = None
27
 
28
+ ### Google Drive Authentication with Service Account(Google Cloud)
29
  def auth_google_docs():
30
+ service_account_info = json.loads(os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON"))
31
+ creds =service_account.Credentials.from_service_account_info(
32
+ service_account_info,
33
+ scopes=SCOPES
34
+ )
35
+ return creds
36
+
37
+ ### Previous Logic (Local Development)
38
+ # def auth_google_docs():
39
+ # global creds
40
+ # if DOCS_TOKEN:
41
+ # try:
42
+ # token_json = json.loads(DOCS_TOKEN)
43
+ # creds = Credentials.from_authorized_user_file(token_json, SCOPES)
44
+ # except Exception:
45
+ # creds = None
46
+
47
+ # if not creds or not creds.valid:
48
+ # if creds and creds.expired and creds.refresh_token:
49
+ # creds.refresh(Request())
50
+ # else:
51
+ # if DOCS_CREDENTIALS:
52
+ # client_config_json = json.loads(DOCS_CREDENTIALS)
53
+ # flow = InstalledAppFlow.run_local_server(
54
+ # client_config_json, SCOPES, port=0
55
+ # )
56
 
57
+ # creds = flow.run_console()
58
 
59
+ # return creds
60
 
61
 
62
  def get_docs_text() -> List[Document]: