代码之家  ›  专栏  ›  技术社区  ›  Py.Jordan

Google-目录API(Admin SDK)创建组-错误

  •  1
  • Py.Jordan  · 技术社区  · 7 年前

    我目前正在关注谷歌的 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' # 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()
    

    感谢您抽出时间阅读这篇文章,我希望很快能收到您的回复,

    有希望的程序员。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Vikram Shinde    7 年前

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

        2
  •  1
  •   Py.Jordan    7 年前

    我在查看了Google云项目中的错误日志后找到了解决方案,这只是一个简单的修复。 之前:

    group = service.users().insert(body = test).execute()
    

    之后:

    group = service.groups().insert(body = test).execute()
    

    在这里你可以看到我换了服务。用户进行服务。组。

    我不敢相信我让这样一个简单的错误过去了,但离开这里,以防万一这会帮助别人。

    非常感谢。