【问题标题】:access user profile information in a google domain with python使用 python 访问谷歌域中的用户个人资料信息
【发布时间】:2021-04-23 22:05:37
【问题描述】:

目标:我的 python 网络应用程序从客户那里访问谷歌用户的信息。他们已经向服务帐户提供了访问 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">

知道可能出了什么问题吗?

【问题讨论】:

    标签: python flask google-cloud-platform google-oauth api-key


    【解决方案1】:

    您不能在同一个 GET 请求上使用 domaincustomer。在 Admin Google API 文档中,它为 domain 字段声明了以下内容:

    • 域名。使用此字段仅从一个域中获取字段。要返回客户帐户的所有域,请改用客户查询参数。必须提供客户或域参数。

    【讨论】:

    • 它解决了错误的请求问题,谢谢,现在 403 未授权,但我要求域所有者为我们正在使用的应用程序凭据添加授权范围 admin.directory.user.readonly,我我还缺少什么?非常感谢
    • 假设您的 OAuth 请求授权设置正确,您的问题可能与其他问题有关:stackoverflow.com/questions/42243221/…
    • @GustavoCastanheira - 这个答案加上你需要模仿。 creds = PASS_CREDENTIALS.with_subject(EMAIL_ADDRESS)
    • @GustavoCastanheira - 此链接有助于理解我的评论:developers.google.com/admin-sdk/directory/v1/guides/…
    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2014-04-19
    相关资源
    最近更新 更多