2014-12-03 107 views
7

授权客户端还是范围试图运行我的代码,当我得到这个错误: 谷歌API的Python unauthorized_client:在请求

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='[email protected]') 

    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权限的超级管理员。

为什么我会得到这个错误?

+0

https://developers.google.com/admin-sdk/directory/v1/guides/代表团我认为你在创建服务帐户和凭证下忘记了第一步。 – DaImTo 2014-12-04 13:52:15

回答

1

您还需要转到该域的G Suite Admin,然后单击安全性,显示更多信息,高级设置,管理Api客户端访问权限(或在撰写本文时仅浏览至this)。

然后添加一个条目,该条目在客户名称中包含您的客户名称,并且范围具有您的范围。比如我的是这样,你并不需要所有范围只有一个适合你的目的:

enter image description here