Google api 是否可以从服务帐户调用API,而无需在域范围的委派中代表用户?
我的Python脚本从G套件中检索关于用户和组的信息(目前,使用14天免费帐户进行测试)。我正在为服务器到服务器应用程序使用域范围的委派和OAuth 2.0,因为我不想显示一个弹出窗口,其中来自托管域的用户将允许我查看他们的组和用户 以下是我获取用户和组所遵循的步骤:Google api 是否可以从服务帐户调用API,而无需在域范围的委派中代表用户?,google-api,google-oauth,google-admin-sdk,service-accounts,Google Api,Google Oauth,Google Admin Sdk,Service Accounts,我的Python脚本从G套件中检索关于用户和组的信息(目前,使用14天免费帐户进行测试)。我正在为服务器到服务器应用程序使用域范围的委派和OAuth 2.0,因为我不想显示一个弹出窗口,其中来自托管域的用户将允许我查看他们的组和用户 以下是我获取用户和组所遵循的步骤: 创建并下载所有必要的凭据,如客户端ID 从my G Suite管理控制台,允许访问我的客户端ID,并授予其访问与我的脚本具有相同作用域的用户和组API的权限 在我的脚本中,使用.json创建凭据,并代表G套件管理员发出请求 开始调
from google.oauth2 import service_account
import googleapiclient.discovery
import json
""" CONSTANTS AND GLOBAL VARIABLES
"""
# The API we request to use
SCOPES = ['https://www.googleapis.com/auth/admin.directory.group.readonly',
'https://www.googleapis.com/auth/admin.directory.user.readonly']
# json containing keys, account service email, id client and other stuff
SERVICE_ACCOUNT_FILE = 'my_file.json'
# The hosted domain we want to work with
DOMAIN = 'some_hd.it'
# The user I'm using to create credentials
USER_EMAIL = 'name.surname@some_hd.it'
""" SETTING AND GETTING CREDENTIALS
"""
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
if credentials is None:
print "BAD CREDENTIALS"
delegated_credentials = credentials.with_subject(USER_EMAIL)
"""
build('api_name', 'api_version', ...)
https://developers.google.com/api-client-library/python/apis/
"""
service = googleapiclient.discovery.build('admin', 'directory_v1',
credentials=delegated_credentials)
""" GETTING GROUPS AND USERS
"""
request = service.groups().list(domain=DOMAIN)
response = request.execute()
groups = response.get('groups', [])
if not groups:
print "No groups in %s" % (DOMAIN)
print
request = service.users().list(domain=DOMAIN)
response = request.execute()
users = response.get('users', [])
if not users:
print "No users in %s" % (DOMAIN)
else:
for user in users:
for email in user['emails']:
print email['address']
print 'User ID: %s' % (user['id'])
print 'Is admin? %s' % (str(user['isAdmin']))
print
我已经使用GoogleAPI有一段时间了,我们有相同的场景,不,据我所知,你需要模拟 请注意,仅仅允许API、下载凭据并向客户机ID授予scopes权限是不够的
对于某些服务,如目录API,您需要向要从admin gsuite面板模拟的用户授予特定的读写权限,以便能够访问您的组和用户。Ugh,这意味着在某个时候,我的程序应该要求托管域管理员授予我的客户端ID我需要的作用域。如果这是我能实现的最有计划的方式,我会接受。是的,就是这样。在这里,我们访问某些域组以管理用户和OU,对于特定功能,我们使用具有该功能特定作用域的服务管理员用户创建服务帐户凭据。