试图使用管理目录API从Google组中删除用户时出现HttpError 403

2024-04-16 14:45:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个新的Python用户,正在开发一些Python代码来管理Google组。 我是这个帐户的管理员。 我已经成功地将用户添加到组中,但是在调用delete函数时出现了HTTP403错误

目前,我正在硬编码客户端ID和客户端密码。(在代码示例中使用…) 我相信我使用了正确的范围

from apiclient.discovery import build
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow

client_id = "......."
client_secret = "....."
scope = 'https://www.googleapis.com/auth/admin.directory.group'
flow = OAuth2WebServerFlow(client_id, client_secret, scope)
storage = Storage('credentials.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, tools.argparser.parse_args())
http = httplib2.Http()
http = credentials.authorize(http)
service = build('admin', 'directory_v1', http=http)

group_name="testgroup@xyz.com"
user_email = "testuser@xyz.com"

此插入用户的调用可用于:

groupinfo = {'email':user_email}
service.members().insert(groupKey=group_name, body=groupinfo).execute()

此删除请求生成403错误

service.members().delete(groupKey=group_name, memberKey=user_email).execute()

欢迎提出意见/建议