gpt4 book ai didi

python - Google Admin SDK 错误资源未找到 : domain when trying to list existing users

转载 作者:太空宇宙 更新时间:2023-11-04 01:11:59 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的脚本来使用 Google 的 python API 获取我的 Google Apps 用户列表。到目前为止它看起来像这样(基于 Google 示例):

!/usr/bin/python

import httplib2

from apiclient import errors
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import SignedJwtAssertionCredentials

client_email = 'service_account_email@developer.gserviceaccount.com'
with open("Python GAPS-98dfb88b4c9f.p12") as f:
private_key = f.read()

OAUTH_SCOPE = 'https://www.googleapis.com/auth/admin.directory.user'

credentials = SignedJwtAssertionCredentials(client_email, private_key, OAUTH_SCOPE )
http = httplib2.Http()
http = credentials.authorize(http)

directory_service = build('admin', 'directory_v1', http=http)

all_users = []
page_token = None
params = {'customer': 'my_customer'}

while True:
try:
if page_token:
param['pageToken'] = page_token
current_page = directory_service.users().list(**params).execute()

all_users.extend(current_page['users'])
page_token = current_page.get('nextPageToken')
if not page_token:
break
except errors.HttpError as error:
print 'An error occurred: %s' % error
break

for user in all_users:
print user['primaryEmail']

该服务帐户已在 Google 开发者控制台上获得以下 API 的授权:

https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.alias

但是,当我运行代码时,出现了这个错误:

An error occurred: <HttpError 404 when requesting https://www.googleapis.com/admin/directory/v1/users?customer=my_customer&alt=json returned "Resource Not Found: domain"> 

关于我遗漏了什么的任何提示?

E.

最佳答案

即使在使用服务帐户时,您仍然需要“充当”域中具有适当权限(例如 super 管理员)的 Google Apps 用户。尝试:

credentials = SignedJwtAssertionCredentials(client_email, private_key,
OAUTH_SCOPE, sub='admin@domain.com')

其中 admin@domain.com 是您域中 super 管理员的电子邮件地址。

关于python - Google Admin SDK 错误资源未找到 : domain when trying to list existing users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27175867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com