簡體   English   中英

通過域范圍委派模擬用戶的服務帳戶訪問 Google Sheets API 失敗

[英]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

使用域范圍委派時要考慮

默認情況下不啟用域范圍的委派。 要允許它,您需要按照文檔中描述的步驟進行操作。

  1. 步驟:在 GCP 控制台中:
  • 您需要為要用於此目的的每個服務帳戶激活復選框Enable G Suite Domain-wide Delegation要使用服務帳戶模擬用戶,您需要在管理控制台中授予必要的權限
  1. 在管理控制台中:
  • 必須在管理控制台中授權服務帳戶在模擬用戶時所需的任何范圍
  • 為此,請轉到Main menu menu> Security > API controls.
  • 通過其Client ID添加(如果尚未完成)感興趣的服務帳戶,為其提供所需的所有范圍並授權
  • 如果需要,您可以在以后隨時修改范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM