Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,8 @@ from googleapiclient.errors import HttpError
|
|
| 11 |
from langchain.schema import Document
|
| 12 |
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
|
| 18 |
SCOPES = [
|
|
@@ -33,21 +33,24 @@ creds = None
|
|
| 33 |
### Google Drive Authentication
|
| 34 |
def auth_google_docs():
|
| 35 |
global creds
|
| 36 |
-
if
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 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 |
|