【问题标题】:HttpError 403 when attempting to use Admin Directory SDK尝试使用管理目录 SDK 时出现 HttpError 403
【发布时间】:2015-04-14 02:23:03
【问题描述】:

我正在尝试使用 apiclient 与我的 Google Apps for Education 帐户进行交互。我正在使用 服务帐户作为最终目标是将其与我们现有的内部系统(即具有控制台访问权限的基于服务器的系统)联系起来。

我的代码是:

from httplib2 import Http
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

import pprint

with open('/path/mykey.p12') as f:
    key = f.read()

client_email = "my_email@developer.gserviceaccount.com"

scope = ['https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/admin.directory.group']

http = Http()
credentials = SignedJwtAssertionCredentials(client_email, key, scope=scope)
credentials.authorize(http)

admin = build('admin', 'directory_v1', http=http)
users = admin.users.list(domain="mydomain.edu").execute(http=http)
pprint.pprint(users)

当我运行我的代码时,我得到以下回溯:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/ajford/.virtualenvs/aosa_usertools/local/lib/python2.7/site-packages/oauth2client/util.py", line 135, in positional_wrap
per
    return wrapped(*args, **kwargs)
  File "/home/ajford/.virtualenvs/aosa_usertools/local/lib/python2.7/site-packages/googleapiclient/http.py", line 723, in execute
    raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users?domain=mydomain.edu&alt=json returned "Not Authorized to access this resource/api">

我已在 Apps Admin Console 的安全页面上输入了我的 API 访问信息。我试过添加sub=superadmin@mydomain.edu。 我已确保在开发人员控制台中的项目上启用了 Admin SDK。我已经确定我在域下的用户帐户有 Admin Roles 下的适当角色(以防万一)。

我已经在 API Explorer 下尝试了我的测试查询,它运行良好。

我不知道从这里去哪里。有什么建议?

【问题讨论】:

  • 为什么需要使用服务帐号?
  • @Jay Lee 最终产品将在服务器的后台异步运行。我不希望有人必须定期登录。还是我误解了网络令牌系统的工作原理?

标签: python-2.7 google-api google-admin-sdk google-api-python-client


【解决方案1】:

该不该声明

users = admin.users.list(domain="mydomain.edu").execute(http=http)

成为

users = admin.users().list(domain="mydomain.edu").execute(http=http)

【讨论】:

    【解决方案2】:

    您无需使用服务帐号。使用installed application 流并获取刷新令牌。只要不被撤销,授权就会持续。只有访问令牌过期,可以通过刷新令牌进行更新。访问令牌刷新由客户端库自动处理,因此您的工作量减少了。

    【讨论】:

      猜你喜欢
      • 2017-09-13
      • 1970-01-01
      • 2016-08-01
      • 2021-12-08
      • 1970-01-01
      • 2014-06-04
      • 2021-01-28
      • 2014-09-22
      • 1970-01-01
      相关资源
      最近更新 更多