Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update authorization library for Google Sheets to support account delegation #11

Merged
merged 2 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions parsons/google/google_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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',
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down