![](/img/trans.png)
[英]Google API Service Account. Can only see service accounts drive even with Domain Wide Delegation Access
[英]Accessing Google Sheets API through a service account impersonating a user through domain-wide delegation fails
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')
在最后一行失败Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.
最后一行提供了一个用于访问 Google Sheets 的对象。
subject
帐户在域上subject
帐户共享subject
并直接与服务帐户共享工作表时,它会起作用环境
python==3.6.9
pygsheets==2.0.3.1
google-auth==1.6.3
默认情况下不启用域范围的委派。 要允许它,您需要按照文档中描述的步骤进行操作。
Enable G Suite Domain-wide Delegation
要使用服务帐户模拟用户,您需要在管理控制台中授予必要的权限Main menu menu> Security > API controls.
Client ID
添加(如果尚未完成)感兴趣的服务帐户,为其提供所需的所有范围并授权
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.