我正在尝试通过Google管理目录API列出用户 .
import logging
import os
from google.appengine.api import memcache
from googleapiclient import discovery
from oauth2client.contrib.appengine import AppAssertionCredentials
import httplib2
from flask import Flask
credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/admin.directory.user')
auth_http = credentials.authorize(httplib2.Http(cache=memcache))
service = discovery.build('admin', 'directory_v1', http=auth_http)
@app.route('/list')
def list():
results = service.users().list(domain='example.com', maxResults=10, orderBy='email').execute()
return 'success'
app = Flask(__name__)
我在App Engine中运行此功能,并按照https://developers.google.com/api-client-library/python/auth/service-accounts中的说明为App Engine默认服务帐户启用了域范围的委派
这是我得到的错误:HttpError:https://www.googleapis.com/admin/directory/v1/users?orderBy = email@domain=example.com&alt=json&maxResults=10返回"Not Authorized to access this resource/api">
1 回答
按照Delegating domain-wide authority to the service account中指示的步骤操作:
然后,G Suite域的管理员必须完成以下步骤:
转到G Suite域的管理控制台 .
从控件列表中选择安全性 . 如果您没有看到控件,请确保您已以域管理员身份登录 .
从选项列表中选择显示更多,然后选择高级设置 .
在“身份验证”部分中选择“管理API客户端访问” .
在“客户名称”字段中,在“服务帐户”页面中输入服务帐户's Client ID. You can find your service account'的客户端ID .
在“一个或多个API范围”字段中,输入应授予应用程序访问权限的范围列表 . 例如,如果您的应用需要对Google Drive API和Google Calendar API进行全域访问,请输入:https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/calendar .
单击“授权” .
Make sure your service account is set to Administrator.