希望你能提供帮助。
尝试通过服务帐户与GSuite API进行交互;然而,使用这种方法使客户未经授权检索访问令牌#39;错误。
以下示例代码:
#!/usr/bin/env python3
# Set env, import tools and generally get started
from __future__ import print_function
import httplib2
import os
from httplib2 import Http
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.service_account import ServiceAccountCredentials
from oauth2client.service_account import _JWTAccessCredentials
try:
import argparse
flags = argparse.ArgumentParser(parents= .
[tools.argparser]).parse_args()
except ImportError:
flags = None
service_credentials_file =
'/Users/my.user/Documents/Github/GSuite/service_credential.json'
# change working directory to script location (required for file use)
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
scopes = ['https://www.googleapis.com/auth/admin.directory.user',]
def test_function():
credentials = ServiceAccountCredentials.from_json_keyfile_name(service_credentials_file, scopes=scopes)
delegated_credentials =
credentials.create_delegated('super.admin@domain.co.uk')
http_auth = delegated_credentials.authorize(httplib2.Http())
service = discovery.build('admin', 'directory_v1', http=http_auth)
print('Getting the first 10 users in the domain')
results = service.users().list(customer='my_customer', maxResults=10, orderBy='email', domain='nnn.nn').execute()
print(results)
if __name__ == '__main__':
test_function()
引发完整错误:
引发HttpAccessTokenRefreshError(error_msg,status = resp.status) oauth2client.client.HttpAccessTokenRefreshError:unauthorized_client:客户端未经授权使用此方法检索访问令牌。
到目前为止我检查过的内容:
这里的任何帮助都会令人惊叹,因为它已经过了一周时间,并且我完全没有想法(就像谷歌博士一样)。
谢谢!