Example code
from google.oauth2 import service_account
import pygsheets
creds = service_account.Credentials.from_service_account_file(
'my/path/to/credentials.json',
scopes=('https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'),
subject='account@mydomain.com'
)
pg = pygsheets.authorize(custom_credentials=creds)
pg.open_by_url('https://docs.google.com/spreadsheets/d/my_spreadsheet_id/edit#gid=my_sheet_id')
Problematic behaviour
Fails on the last line with Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.
Expected behaviour
The last line provides an object for Google Sheets access.
Additional info
- Domain-wide delegation is enabled for the service account,
subjectaccount is on the domain - Sheet is shared with the
subjectaccount - When I don't provide the
subjectand share the sheet with the service account directly, it works
Environment
python==3.6.9pygsheets==2.0.3.1google-auth==1.6.3