mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-10 15:06:18 +00:00
Allow GoogleDrive to authenticate via application default credentials on Cloud Run/GCE etc without service key (#6035)
@eyurtsev The existing GoogleDrive implementation always needs a service account to be available at the credentials location. When running on GCP services such as Cloud Run, a service account already exists in the metadata of the service, so no physical key is necessary. This change adds a check to see if it is running in such an environment, and uses that authentication instead. --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
parent
6f36f0f930
commit
b7ba7e8a7b
@ -9,6 +9,7 @@
|
|||||||
# 4. For service accounts visit
|
# 4. For service accounts visit
|
||||||
# https://cloud.google.com/iam/docs/service-accounts-create
|
# https://cloud.google.com/iam/docs/service-accounts-create
|
||||||
|
|
||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, List, Optional, Sequence, Union
|
from typing import Any, Dict, List, Optional, Sequence, Union
|
||||||
|
|
||||||
@ -91,6 +92,7 @@ class GoogleDriveLoader(BaseLoader, BaseModel):
|
|||||||
"""Load credentials."""
|
"""Load credentials."""
|
||||||
# Adapted from https://developers.google.com/drive/api/v3/quickstart/python
|
# Adapted from https://developers.google.com/drive/api/v3/quickstart/python
|
||||||
try:
|
try:
|
||||||
|
from google.auth import default
|
||||||
from google.auth.transport.requests import Request
|
from google.auth.transport.requests import Request
|
||||||
from google.oauth2 import service_account
|
from google.oauth2 import service_account
|
||||||
from google.oauth2.credentials import Credentials
|
from google.oauth2.credentials import Credentials
|
||||||
@ -116,6 +118,12 @@ class GoogleDriveLoader(BaseLoader, BaseModel):
|
|||||||
if not creds or not creds.valid:
|
if not creds or not creds.valid:
|
||||||
if creds and creds.expired and creds.refresh_token:
|
if creds and creds.expired and creds.refresh_token:
|
||||||
creds.refresh(Request())
|
creds.refresh(Request())
|
||||||
|
elif "GOOGLE_APPLICATION_CREDENTIALS" not in os.environ:
|
||||||
|
creds, project = default()
|
||||||
|
creds = creds.with_scopes(SCOPES)
|
||||||
|
# no need to write to file
|
||||||
|
if creds:
|
||||||
|
return creds
|
||||||
else:
|
else:
|
||||||
flow = InstalledAppFlow.from_client_secrets_file(
|
flow = InstalledAppFlow.from_client_secrets_file(
|
||||||
str(self.credentials_path), SCOPES
|
str(self.credentials_path), SCOPES
|
||||||
|
Loading…
Reference in New Issue
Block a user