From 80d8db5f60c09184fd4718d39c7e561f97cb7108 Mon Sep 17 00:00:00 2001 From: Kevin <58534537+K-minutti@users.noreply.github.com> Date: Sat, 18 Mar 2023 22:55:17 -0400 Subject: [PATCH] Add service account support to Google Drive (#1761) Having service account support in the drive document loader would be nice. This is already present in the youtube loader. https://github.com/hwchase17/langchain/blob/cb646082baa173fdee7f2b1e361be368acef4e7e/langchain/document_loaders/youtube.py#L76-L78 --- langchain/document_loaders/googledrive.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/langchain/document_loaders/googledrive.py b/langchain/document_loaders/googledrive.py index cec86f50..d6c8951e 100644 --- a/langchain/document_loaders/googledrive.py +++ b/langchain/document_loaders/googledrive.py @@ -6,7 +6,8 @@ # https://console.cloud.google.com/flows/enableapi?apiid=drive.googleapis.com # 3. Authorize credentials for desktop app: # https://developers.google.com/drive/api/quickstart/python#authorize_credentials_for_a_desktop_application # noqa: E501 - +# 4. For service accounts visit +# https://cloud.google.com/iam/docs/service-accounts-create from pathlib import Path from typing import Any, Dict, List, Optional @@ -22,6 +23,7 @@ SCOPES = ["https://www.googleapis.com/auth/drive.readonly"] class GoogleDriveLoader(BaseLoader, BaseModel): """Loader that loads Google Docs from Google Drive.""" + service_account_key: Path = Path.home() / ".credentials" / "keys.json" credentials_path: Path = Path.home() / ".credentials" / "credentials.json" token_path: Path = Path.home() / ".credentials" / "token.json" folder_id: Optional[str] = None @@ -60,6 +62,7 @@ class GoogleDriveLoader(BaseLoader, BaseModel): # Adapted from https://developers.google.com/drive/api/v3/quickstart/python try: from google.auth.transport.requests import Request + from google.oauth2 import service_account from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow except ImportError: @@ -72,6 +75,11 @@ class GoogleDriveLoader(BaseLoader, BaseModel): ) creds = None + if self.service_account_key.exists(): + return service_account.Credentials.from_service_account_file( + str(self.service_account_key), scopes=SCOPES + ) + if self.token_path.exists(): creds = Credentials.from_authorized_user_file(str(self.token_path), SCOPES)