![](/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.