【问题标题】:What is the proper way to authenticate with Google to use Google Directory API in the Admin SDK?向 Google 进行身份验证以在 Admin SDK 中使用 Google Directory API 的正确方法是什么?
【发布时间】:2013-11-23 08:46:31
【问题描述】:

过去几天,我在使用 Admin SDK for Google Apps 中的 Google Directory API 时遇到了问题。该文档还有很多不足之处,当我联系 Google Apps Enterprise 支持时,他们表示他们不支持 API。我正在使用 Google 提供的最新 Python API 客户端库,因为他们认为这是最好的方法。我已登录 Google API 控制台并创建了一个服务帐户并下载了 OAuth2 密钥。我还在控制台中打开了 Admin SDK。这是我的代码:

f = file("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12", "rb")
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials(
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com",
    key,
    scope = "https://www.googleapis.com/auth/admin.directory.orgunit"
)

http = httplib2.Http()
http = credentials.authorize(http)

directoryservice = build("admin", "directory_v1", http=http)

orgunits = directoryservice.orgunits().list(customerId='XXXXXXX').execute(http=http)
pprint.pprint(orgunits)

请注意,customerId 是我们的 Google Apps 客户 ID。我用“my_customer”尝试过,因为谷歌似乎表明在使用超级管理员帐户时应该可以工作,但是当我尝试这种方式时,我收到了返回“invalid customerId”。所以我硬编码了我们实际的customerId。

当 harcoded 总是收到返回“需要登录”但似乎身份验证过程正在工作,因为目录对象是通过 build 命令创建的。难道我做错了什么?

注意,我还在某处读到,有时请求需要来自域帐户而不是服务帐户,为此您需要添加:

sub = "domain_account_superadmin@example.com"

在 SignedJwtAssertionCredentials 调用中...我尝试过,但随后收到消息“access_denied”

提前感谢您的建议。

【问题讨论】:

    标签: python google-oauth google-api-python-client google-admin-sdk google-directory-api


    【解决方案1】:

    在此处查看 google 驱动器示例:https://developers.google.com/drive/delegation 不要忘记为服务帐户和范围委派域范围的权限。 以下是通过服务帐号列出组织单位的示例:

    import sys
    import apiclient.discovery
    import oauth2client.client
    import httplib2
    import pprint
    
    # see example for using service account here: 
    #   https://developers.google.com/drive/delegation
    def main (argv):
        scopes = ('https://www.googleapis.com/auth/admin.directory.orgunit')
        service_account_email = 'xxx@developer.gserviceaccount.com'
        acting_as_user = 'yyy@zzz' # must have the privileges to view the org units
        f = file('key.p12', 'rb')
        key = f.read()
        f.close()
        credentials = oauth2client.client.SignedJwtAssertionCredentials(
            service_account_email,
            key,
            scope=scopes,
            sub=acting_as_user
            )
        http = httplib2.Http()
        http = credentials.authorize(http)
        directoryservice = apiclient.discovery.build('admin', 'directory_v1', http=http)
        response = directoryservice.orgunits().list(customerId='my_customer').execute(http=http)
        pprint.pprint(response)
    
    if __name__ == '__main__':
        main(sys.argv)
    

    【讨论】:

    • 感谢您的回复。这与我的代码完全相同,当我尝试添加“sub=xxx@zzz”时,我使用此代码“access_denied”收到相同的消息......当您说“不要忘记为服务委派域范围的权限时帐户和范围”这是什么意思?目前,我是我正在测试的 Google Apps 域的“超级管理员”角色......我是否缺少让我对访问权限感到不满的应用程序控制台?
    • 没关系...切换到旧视图时,我终于在管理控制台中找到了它。整个时间都是因为我没有在那里委派适当的权限。很简单的问题,但如果不是你一开始的简单提醒,我不会得到这个。再次感谢!
    • @Tyler 我面临着几乎相同的问题——当您说管理控制台的“旧视图”时,您指的是旧版云控制台还是旧版应用管理控制台? (我只看到过新版本。)在哪里可以委派权限?
    • @EricWalker “旧视图”是指应用程序管理控制台。这是因为在我的大学,我们有两个域,一个用于测试迁移过程,另一个是我们在 Google Apps 上的“生产”域。有趣的是,我们的生产域使用新的管理控制台,但我们在测试域上没有可用的新控制台。它根本不允许我们使用它。在 OLD 管理控制台中,我在“高级工具”选项卡 ->“管理第三方 OAuth 客户端访问”中找到了我需要的内容
    • @EricWalker 此屏幕截图显示了您输入客户端 ID 的位置以及您希望该客户端能够访问的范围。 i.tylerwhitney.com/image/2I3y39270z29 请注意,客户端 ID 是域,而不是电子邮件地址。这有点令人困惑。最后,考虑到它的价值,我也选择使用旧的 API 控制台(现在称为 Cloud Console)。主要是因为我在尝试添加新的 OAuth2 服务帐户时感到很痛苦……而且它在旧 API 控制台中更加直观。
    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 2019-05-14
    • 2020-05-21
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多