谷歌 API Python 未经授权的客户端:请求中未经授权的客户端或范围 [英] Google API Python unauthorized_client: Unauthorized client or scope in request
问题描述
尝试运行我的代码时出现此错误:
oauth2client.client.AccessTokenRefreshError:未授权的客户端:请求中的未授权客户端或范围.
这是我的代码:
导入json进口请求导入 httplib2从 oauth2client.client 导入 SignedJwtAssertionCredentials从 apiclient.discovery 导入构建如果 __name__ == '__main__':json_key_file = 'my-key.json'使用 open(json_key_file) 作为 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)数据 = service.users().list(domain='domain.com').execute()打印数据
我在控制台中正确设置了范围,并且在控制台中启用了我的 Admin SDK.我的电子邮件是超级管理员,可以访问所有 Admin API 权限.
为什么我会收到这个错误?
想通了:
在设置 API 范围时,您需要使用开发者控制台"中的客户端 ID 作为管理 API 客户端访问"中的客户端名称
https://developers.google.com/+/domains/authentication/delegation>
I get this error when trying to run my code:
oauth2client.client.AccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request.
Here is my code:
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
I have the scope set correctly in my console, and I have my Admin SDK enabled in my console. My email is a super admin with access to all Admin API Privileges.
Why would I be getting this error?
Figured it out:
You need to use the client ID from your "Developers Console" as the Client Name in the "Manage API client access" when you're setting your API scopes
https://developers.google.com/+/domains/authentication/delegation
这篇关于谷歌 API Python 未经授权的客户端:请求中未经授权的客户端或范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!