尝试通过 Python 中的服务帐户使用AdminSettings API来检索用户的域名数,GetCurrentNumberOfUsers()'启用委派范围广泛的权限和范围,但是获得错误。我使用了Calendar API,Directory API,EmailSettings API的服务帐户,但不适用于AdminSettings。尝试了以下示例代码: github.com/Khan/gdata-python-client/blob/master/samples/apps/adminsettings_example.py 但需要获得授权'使用管理员帐户的正确凭据时出错:API Client Acccess
from oauth2client.client import SignedJwtAssertionCredentials
import gdata.gauth
import gdata.apps.service
import gdata.apps.adminsettings.service
SERVICE_ACCOUNT_EMAIL = "XXXXXXXXXXXXX-ebirq08jvhldahbb482u8a1otu9n3l8p.apps.googleusercontent.com"
SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'gapi_admin/privatekey.p12'
f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key, scope='https://apps-apis.google.com/a/feeds/domain/', sub='admin@xxxtestmail.edu')
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
service = gdata.apps.adminsettings.service.AdminSettingsService(source="testApp", domain='xxxtestmail.edu')
service = auth2token.authorize(service)
print service.GetCurrentNumberOfUsers()
#output
#TypeError: new_request() takes exactly 1 non-keyword argument (2 given)
在OAuth2 Playground中运行正常,查看Screenshot。
答案 0 :(得分:0)
旧的GData Python库服务对象实际上并不支持您需要使用的OAuth 2.0。但是你可以在那里破解访问令牌。尝试类似:
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key, scope='https://apps-apis.google.com/a/feeds/domain/', sub='admin@xxxtestmail.edu')
credentials.refresh(httplib2.Http())
service = gdata.apps.adminsettings.service.AdminSettingsService(source="testApp", domain='xxxtestmail.edu')
service.additional_headers[u'Authorization'] = u'Bearer {0}'.format(credentials.access_token)
print service.GetCurrentNumberOfUsers()