我一直试图让服务帐户身份验证为Google Admin SDK工作几天,但无济于事.我正在使用谷歌新安装的google-api-python-client-1.2库.
我一直在关注该主题的Google文档.链接在这里:
HTPS://developers.google.com/accounts/docs/OAuth2ServiceAccount
HTPS://developers.google.com/api-client-library/python/guide/aaa_oauth
HTP://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.SignedJwtAssertionCredentials-class.html
并让tasks.py服务帐户示例正常工作,您可以在此处找到:
HTP://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py R = c21573904a2df1334d13b4380f63463c94c8d0e8
并且一直在密切研究这两个相关主题的Stack Overflow线程:
google admin sdk目录api 403 python
使用Oauth2作为服务帐户(教育版)的Google管理员API - 403错误
并研究了gam.py(Dito GAM)中的相关代码.
然而,我仍然遗漏了一些东西,因为我在几乎每个编写的测试用例中都收到了'oauth2client.client.AccessTokenRefreshError:access_denied'异常.
以下是测试身份验证的简明示例:
import httplib2
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
f = file('myKey.p12', 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(
'myServiceAdmin@developer.gserviceaccount.com',
key,
sub='myAdminUser@my.googleDomain.edu',
scope = ['https://www.googleapis.com/auth/admin.directory.user',])
http = httplib2.Http()
http = credentials.authorize(http)
service = build('admin', 'directory_v1', http=http)
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我得到了这个堆栈转储和异常:
Traceback (most recent call last):
File "./test.py", line 17, in <module>
service = build('admin', 'directory_v1', …Run Code Online (Sandbox Code Playgroud)