我目前正在关注谷歌的
Python Quickstart for Directory API
并尝试使用REST API的不同方法。本教程设计为在命令行中运行,并通过创建一个“我已成功更改了教程的代码”来使用O Auth 2.0进行身份验证,以便我可以将用户插入到现有的组中,通过传递我希望插入用户的组的groupKey和包含我添加的用户的电子邮件的正文,但是,我无法获得
Create a Group
工作。
我遇到的错误是:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users?alt=json returned "Insufficient Permission">
我几乎可以肯定,我在谷歌云项目中的权限是正确的,因为我是一名编辑,而在G套件中,我是一名超级管理员。起初,我认为我可能指向了错误的范围,但这似乎是创建组的建议范围。
有人能帮我创建一个小组吗,因为我对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'
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:
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",
"name" : "Test Group",
"description" : "Just testing here."
}
group = service.users().insert(body = test).execute()
return group
if __name__ == '__main__':
main()
感谢您抽出时间阅读这篇文章,我希望很快能收到您的回复,
有希望的程序员。