From 2e2047d25853b4290ab38ab8ca2c35ef480f6339 Mon Sep 17 00:00:00 2001 From: sjwmoveon Date: Fri, 8 Apr 2022 14:18:47 -0400 Subject: [PATCH 1/2] Update authorization library for Google Sheets to support account delegation The oauth2client library is deprecated, and gspread will drop most information from the oauth2client authorization. Switch to the actively maintained google-auth library and add an argument for a delegated account name. --- parsons/google/google_sheets.py | 13 ++++++++----- requirements.txt | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/parsons/google/google_sheets.py b/parsons/google/google_sheets.py index 73fc4d09d2..9b95925427 100644 --- a/parsons/google/google_sheets.py +++ b/parsons/google/google_sheets.py @@ -6,7 +6,7 @@ from parsons.google.utitities import setup_google_application_credentials import gspread -from oauth2client.service_account import ServiceAccountCredentials +from google.oauth2.service_account import Credentials logger = logging.getLogger(__name__) @@ -20,9 +20,11 @@ class GoogleSheets: A dictionary of Google Drive API credentials, parsed from JSON provided by the Google Developer Console. Required if env variable ``GOOGLE_DRIVE_CREDENTIALS`` is not populated. + subject: string + In order to use account impersonation, pass in the email address of the account to be impersonated as a string. """ - def __init__(self, google_keyfile_dict=None): + def __init__(self, google_keyfile_dict=None, subject=None): scope = [ 'https://spreadsheets.google.com/feeds', @@ -33,9 +35,10 @@ def __init__(self, google_keyfile_dict=None): google_credential_file = open(os.environ['GOOGLE_DRIVE_CREDENTIALS']) credentials_dict = json.load(google_credential_file) - credentials = ServiceAccountCredentials.from_json_keyfile_dict( - credentials_dict, scope + credentials = Credentials.from_service_account_info( + credentials_dict, scopes=scope, subject=subject ) + self.gspread_client = gspread.authorize(credentials) def _get_worksheet(self, spreadsheet_id, worksheet=0): @@ -360,7 +363,7 @@ def format_cells(self, spreadsheet_id, range, cell_format, worksheet=0): } }, worksheet=0) - """ # noqa: E501,E261 + """ # noqa: E501,E261 ws = self._get_worksheet(spreadsheet_id, worksheet) ws.format(range, cell_format) diff --git a/requirements.txt b/requirements.txt index fa991c0264..009781ac53 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ psycopg2-binary==2.8.5 xmltodict==0.11.0 gspread==3.7.0 oauth2client==4.1.3 +google-auth=2.6.2 facebook-business==6.0.0 google-api-python-client==1.7.7 google-resumable-media!=0.4.0,<0.5.0dev,>=0.3.1 From ad77645a6b1599598e7eddfc6a945f132b444610 Mon Sep 17 00:00:00 2001 From: sjwmoveon Date: Fri, 8 Apr 2022 14:39:36 -0400 Subject: [PATCH 2/2] Typo fix --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 009781ac53..aaec456acc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ psycopg2-binary==2.8.5 xmltodict==0.11.0 gspread==3.7.0 oauth2client==4.1.3 -google-auth=2.6.2 +google-auth==2.6.2 facebook-business==6.0.0 google-api-python-client==1.7.7 google-resumable-media!=0.4.0,<0.5.0dev,>=0.3.1