【问题标题】:Google API Python unauthorized_client: Unauthorized client or scope in request谷歌 API Python 未授权客户端:请求中未经授权的客户端或范围
【发布时间】:2015-02-01 16:03:47
【问题描述】:

尝试运行我的代码时出现此错误:

oauth2client.client.AccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request.

这是我的代码:

import json
import requests
import httplib2
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient.discovery import build

if __name__ == '__main__':

    json_key_file = 'my-key.json'

    with open(json_key_file) as json_file:

        json_data = json.load(json_file)
        credential = SignedJwtAssertionCredentials(json_data['client_email'], json_data['client_email'], json_data['private_key'], scope=['https://www.googleapis.com/auth/admin.directory.user','https://www.googleapis.com/auth/admin.directory.user.readonly'], sub='myemail@domain.com')

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

    service = build('admin', 'directory_v1', http=http)
    data = service.users().list(domain='domain.com').execute()

    print data

我在我的控制台中正确设置了范围,并且我在我的控制台中启用了我的 Admin SDK。 我的邮箱是超级管理员,可以访问所有 Admin API 权限。

为什么会出现这个错误?

【问题讨论】:

标签: python api python-2.7 google-api


【解决方案1】:

想通了:

在设置 API 范围时,您需要使用“开发者控制台”中的客户端 ID 作为“管理 API 客户端访问”中的客户端名称

https://developers.google.com/+/domains/authentication/delegation

【讨论】:

  • 这个链接好像坏了
【解决方案2】:

您还需要转到域的 G Suite 管理员,然后点击安全、显示更多、高级设置、管理 Api 客户端访问(或在撰写本文时浏览至 this)。

然后添加一个条目,在 Client name 中包含您的客户名称,Scope 包含您的范围。例如我的看起来像这样,您不需要所有范围,只需要适合您目的的一个:

【讨论】:

  • 这仅适用于拥有 G Suite 管理员帐户的人,不适用于免费帐户。
猜你喜欢
  • 2015-07-06
  • 2017-02-22
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2022-09-19
  • 2013-09-05
  • 1970-01-01
  • 2020-04-30
相关资源
最近更新 更多