使用 python 访问 google 域中的用户个人资料信息
access user profile information in a google domain with python
Objective:我的 python 网络应用程序从客户那里访问 google 用户的信息。他们已经授权服务帐户访问 admin.directory.user.readonly 的范围,我使用此服务帐户 JSON 文件作为我的应用程序的凭据。
credential_path = os.path.join(os.path.dirname(app.instance_path), 'cre.json')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user.readonly']
PATH_TO_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
GET_CREDENTIALS = PATH_TO_CREDENTIALS
PASS_CREDENTIALS = service_account.Credentials.from_service_account_file(GET_CREDENTIALS, scopes=SCOPES)
service = build('admin', 'directory_v1', credentials=PASS_CREDENTIALS)
# Call the Admin SDK Directory API
print('Getting the first 10 users in the domain')
results = service.users().list(customer='customer.com', domain='customer.com', maxResults=10, orderBy='email').execute()
users = results.get('users', [])
for user in users:
print(u'{0} ({1})'.format(user['primaryEmail'], user['name']['fullName']))
我得到:
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://admin.googleapis.com/admin/directory/v1/users?customer=nidec-ga.com&domain=nidec-ga.com&maxResults=10&orderBy=email&alt=json returned "Bad Request". Details: "Bad Request">
知道哪里出了问题吗?
您不能在同一个 GET 请求中使用 domain
和 customer
。在 Admin Google API 文档中,domain
字段说明如下:
- 域名。使用此字段仅从一个域中获取字段。要 return 客户帐户的所有域,请改用客户查询参数。必须提供客户或域参数。
Objective:我的 python 网络应用程序从客户那里访问 google 用户的信息。他们已经授权服务帐户访问 admin.directory.user.readonly 的范围,我使用此服务帐户 JSON 文件作为我的应用程序的凭据。 credential_path = os.path.join(os.path.dirname(app.instance_path), 'cre.json') os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user.readonly']
PATH_TO_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
GET_CREDENTIALS = PATH_TO_CREDENTIALS
PASS_CREDENTIALS = service_account.Credentials.from_service_account_file(GET_CREDENTIALS, scopes=SCOPES)
service = build('admin', 'directory_v1', credentials=PASS_CREDENTIALS)
# Call the Admin SDK Directory API
print('Getting the first 10 users in the domain')
results = service.users().list(customer='customer.com', domain='customer.com', maxResults=10, orderBy='email').execute()
users = results.get('users', [])
for user in users:
print(u'{0} ({1})'.format(user['primaryEmail'], user['name']['fullName']))
我得到:
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://admin.googleapis.com/admin/directory/v1/users?customer=nidec-ga.com&domain=nidec-ga.com&maxResults=10&orderBy=email&alt=json returned "Bad Request". Details: "Bad Request">
知道哪里出了问题吗?
您不能在同一个 GET 请求中使用 domain
和 customer
。在 Admin Google API 文档中,domain
字段说明如下:
- 域名。使用此字段仅从一个域中获取字段。要 return 客户帐户的所有域,请改用客户查询参数。必须提供客户或域参数。