【问题标题】:Insufficient Permission creating gmail group using Google Directory API使用 Google Directory API 创建 gmail 组的权限不足
【发布时间】:2018-12-12 06:01:59
【问题描述】:

我想创建包含一些电子邮件/gmail 的组。我正在使用这个guide。这是我创建组的代码:

SCOPES = 'https://www.googleapis.com/auth/admin.directory.group'
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid {
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
}
service = build('admin', 'directory_v1', http=creds.authorize(Http()))

mdig = createetag();

reqbody = {
    "kind": "admin#directory#group",
    "id": "id065468",
    "etag": "%s" % mdig,
    "email": "grpatest065469@gmail.com",
    "name": "Grptest name",
    "directMembersCount": "2",
    "description": "Grptest",
    "adminCreated": "True",
    "aliases": [
        "first@gmail.com",
        "second@gmail.com"
    ],
    "nonEditableAliases": [
    ]
}

# Call the Admin SDK Directory API
print('Creating new group')
group = service.groups()
g = group.insert(body=reqbody).execute()

我没有在浏览器中看到身份验证窗口,不确定是否是导致问题的原因。这是我的错误:

'kind': 'admin#directory#group', 'id': 'id065468', 'etag': "b'\\x9fR\\xe9O\\x93\\x84\\xbe~\\x19\\xef\\xd2DYJ`\\x1d'", 'email': 'grptest065469@gmail.com', 'name': 'Grptest name', 'directMembersCount': '2', 'description': 'Grp test', 'adminCreated': 'True', 'aliases': ['first@gmail.com', 'second@gmail.com'],'nonEditableAliases': []

Creating new group

Traceback (most recent call last):
File ".\creategrp.py", line 105, in <module> main()
File".\creategrp.py", line 75, in main 
    g = group.insert(body=reqbody).execute()
File "C:\dev\cfehome\lib\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
File "C:\dev\cfehome\lib\googleapiclient\http.py", line 849, in execute 
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/groups?alt=json returned "Insufficient Permission">

【问题讨论】:

    标签: python-3.x google-api google-oauth google-api-python-client google-directory-api


    【解决方案1】:

    Groups: insert 需要https://www.googleapis.com/auth/admin.directory.group 范围才能使用它。最重要的是,您已通过身份验证的用户必须有权执行您正在尝试执行的操作。您的代码似乎使用了正确的范围。

    “权限不足”

    可以表示两件事之一。要么

    • 您正在验证的用户无权执行您正在尝试执行的操作
    • 自用户登录后,您已更改范围。

    选项一:

    确保您登录时使用的用户具有 gsuite 帐户的管理员访问权限。或者您可能需要查看下面的服务帐户。

    选项二:

    我不是 python 开发人员,但我知道您正在使用的库。当用户在stored store = file.Storage('token.json') 表示的目录中登录用户的凭据文件时。通过在用户再次回来时执行此操作,您不必要求他们再次登录。如果您更改了范围,您需要做的是找到该文件并将其删除。它应该会弹出并再次征求您的同意。

    服务帐号

    如果您希望在服务器端运行,您可以使用service account 并设置domain wide delegation,这样当脚本运行时服务帐户将能够根据需要应用这些更改。但是,由于您似乎只是在创建一个组,因此如果您登录的用户仍然具有访问权限,则您可能无需费心创建服务帐户并进行设置。

    Google Api Python 客户端文档 -> Using OAuth 2.0 for Server to Server Applications

    【讨论】:

    • 我认为这是选项 1:我进行身份验证的用户无权访问。您知道如何授予用户访问 gsuite 的权限吗?是否与在用户仪表板中启用 Admin SDK api 有关?
    • 我不确定我是否认为这与用户在 Gsuite 中的访问级别有关
    • Admin SDK lets administrators of enterprise domains to view and manage resources like user, groups etc. It also provides audit and usage reports of domain. 我发现这个关于 Admin SDK。我启用它,删除 token.json,再次尝试输出,但它显示“权限不足”。
    • googleapiclient.errors.HttpError: returned "Domain not found."&gt; 好的,我现在收到此错误。
    • 我试图从我的 gmail 发送到另外两封电子邮件,但它说找不到域..
    【解决方案2】:

    您使用的凭据没有权限。这是由与 G Suite 域范围委派和/或服务帐户凭据模拟(缺少)相关的一个或多个因素造成的。

    1. 您需要创建一个服务帐户。请勿修改或更改任何权限。
    2. 您需要将域范围的权限委派给服务帐户。
    3. 使用服务帐户凭据,您可以冒充另一个具有域超级管理员权限并且至少登录过一次 G Suite 并接受条款和条件的用户。

    从这份关于 G Suite 域委派的文档开始:

    Perform G Suite Domain-Wide Delegation of Authority

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2017-04-24
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多