尝试列出现有用户时,Google Admin SDK错误未找到资源:域 [英] Google Admin SDK error Resource Not Found: domain when trying to list existing users
问题描述
我正在尝试编写一个简单的脚本,以使用Google的python API获取我的Google Apps用户列表.到目前为止,它看起来像这样(基于Google的示例):
I am trying to write a simple script to get a list of my Google Apps users using Google's python API. So far it looks like this (based on a Google example):
!/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 Developer Console上获得以下API的授权:
The service account has been authorized on google developer console for the following API's:
https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.user.alias
但是,当我运行代码时,出现此错误:
However, when I run the code, I get this error:
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">
关于我想念什么的任何提示吗?
Any hints on what am I missing?
E.
推荐答案
即使使用服务帐户,您仍需要充当"域中具有适当权限的Google Apps用户(例如,超级管理员).试试:
Even when using a service account, you still need to "act as" a Google Apps user in the domain with the proper rights (e.g. a super admin). Try:
credentials = SignedJwtAssertionCredentials(client_email, private_key,
OAUTH_SCOPE, sub='admin@domain.com')
其中admin@domain.com是您域中超级管理员的电子邮件.
where admin@domain.com is the email of a super admin in your domain.
这篇关于尝试列出现有用户时,Google Admin SDK错误未找到资源:域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!