【发布时间】:2021-03-29 11:47:42
【问题描述】:
我正在编写一个 python 脚本,该脚本在我的域中创建一个新的 google 用户并将其添加到现有组中。为了确保一切设置正确,我想首先列出该组的当前成员。请注意,我已在控制台中启用了 Admin-SDK API 并创建了 OAuth 2.0 凭据。我的代码如下:
- 授权
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.group',
'https://www.googleapis.com/auth/admin.directory.group.member']
creds = None
credsFile = os.path.abspath("pyScripts/googleCreds/credentials.json")
tokenFile = os.path.abspath("pyScripts/googleCreds/token.pickle")
if os.path.exists(tokenFile):
with open(tokenFile, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credsFile, SCOPES)
creds = flow.run_local_server(port=0)
with open(tokenFile, 'wb') as token:
pickle.dump(creds, token)
service = build('admin', 'directory_v1', credentials=creds)
- 创建用户
service.users().insert(body=request).execute()
这里的请求是参数的字典,如姓名和电子邮件,根据需要
- 列出组中的当前成员
service.members().list(groupKey='group@domain.com').execute()
用户已成功创建,但我在尝试列出组成员时收到 403:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://admin.googleapis.com/admin/directory/v1/groups/group%40domain.com/members?alt=json returned "Not Authorized to access this resource/api">
什么可能导致此问题?我特别困惑,因为用户端点工作正常,但成员却不工作。我还应该提到,我在使用 API Explorer 时看到了相同的行为。任何帮助表示赞赏,谢谢!
【问题讨论】:
标签: python oauth-2.0 google-apps google-admin-sdk google-api-python-client