我正在尝试使用 google python SDK 来获取给定用户所属的所有组,但是我在设置将进行 API 调用的凭据范围时遇到问题。
目前相关代码如下所示:
creds, project = google.auth.default(scopes=["https://www.googleapis.com/auth/admin.directory.user.readonly", "https://www.googleapis.com/auth/admin.directory.group.readonly"])
creds.refresh(Request())
service = build("admin", "directory_v1", credentials=creds)
results = service.groups().list(domain="sky.uk",userKey=USER_EMAIL, maxResults=400).execute()
但是,这会导致以下错误消息:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://admin.googleapis.com/admin/directory/v1/groups?domain=sky.uk&userKey=USER_EMAIL&maxResults=400&alt=json returned "Request had insufficient authentication scopes.". Details: "[{'message': 'Insufficient Permission', 'domain': 'global', 'reason': 'insufficientPermissions'}]">
检查正在使用的凭据,似乎我声明的范围实际上并未被设置,就像在令牌上一样
"scope": "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/sqlservice.login https://www.googleapis.com/auth/userinfo.email openid",
我不太确定我在这里做错了什么,如果有人能指出我正确的方向,我将不胜感激
尝试删除本地凭据,然后重新运行代码,但没有效果。我尝试过添加其他范围,但它们似乎也没有设置
首先,请确保您已检查先决条件:
Python 3.10.7 或更高版本
pip 包管理工具
Google Cloud 项目。
已启用 API 访问权限的 Google Workspace 网域。
该域中具有管理员权限的 Google 帐户。
一些常见问题是未启用 Directory API。
您还可以尝试文档中提供的代码示例。您可以尝试使用
https://www.googleapis.com/auth/admin.directory.user
范围来代替。