gpt4 book ai didi

python - Google - 目录 API(管理 SDK)创建组 - 错误

转载 作者:行者123 更新时间:2023-12-01 02:19:26 25 4
gpt4 key购买 nike

我目前正在关注 Google 的 Python Quickstart for Directory API并尝试使用 REST API 的不同方式。本教程设计为在命令行中运行,并通过创建一个 O Auth 2.0 进行身份验证。我已设法更改本教程的代码,以便我可以通过传递我希望将用户插入到包含我要添加的用户的电子邮件的正文中,但是,我无法获取 Create a Group上类。

我收到的错误是:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users?alt=json returned "Insufficient Permission">

我几乎确信我在 Google Cloud 项目中的权限是正确的,因为我是一名编辑,而在 G Suite 中我是一名 super 管理员。起初我以为我可能指向了错误的范围,但看起来这就是创建组的推荐范围。

有人可以帮助我创建一个小组吗,因为我对 REST API 非常陌生,并且希望提高自己的技能和理解。是否有一个地方我只是误解了,需要回去继续阅读。

我已在下面发布了我的代码:

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None

SCOPES = 'https://www.googleapis.com/auth/admin.directory.group'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'generic-app-name' # removed for example - imagine the name is included


def get_credentials():

home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'admin-directory_v1-python-quickstart.json')

store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials

def main():

credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('admin', 'directory_v1', http=http)

print('Creating Group For Testing')
test = {
"email" : "test@test.co.uk", # Fake email for example
"name" : "Test Group",
"description" : "Just testing here."
}
group = service.users().insert(body = test).execute()
return group

if __name__ == '__main__':
main()

感谢您花时间阅读这篇文章,我希望很快能收到你们中的一些人的回复,

一个充满希望的程序员。

最佳答案

您可以点击链接The Google Directory Groups API for python用于在 Google Directory 中创建群组。

关于python - Google - 目录 API(管理 SDK)创建组 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48094939/

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