google-api-python-client - 使用 flow_from_clientsecrets() 指定 access_type=online

标签 google-api-python-client

我有一个已批准作为具有 openidemail 范围的授权 API 客户端的应用程序。我仍然看到“您的域管理员已批准访问”初始屏幕 as documented here 。解决方案是不通过 access_type 参数请求离线访问。

我的授权流程是使用 flow_from_clientsecrets() 构建的,我没有看到任何方法可以像 OAuth2WebServerFlow 那样将访问类型作为参数传递。有没有办法指定应用程序不需要客户端库的刷新 token ?

最佳答案

我自己刚刚开始使用 Google API Python 客户端库,发现自己处于与您完全相同的情况。根据我的阅读,flow_from_clientsecrets() 仅返回一个应用了一组基本参数的Flow。事实证明,创建您自己的 OAuth2WebServerFlow 对象并应用您需要的任何额外参数要简单得多。

这是我在仔细研究 googleapiclient 和 oauth2client 库中的示例后拼凑出的解决方案:

# Returns a flow that will prompt for offline access, thus receiving
# a refresh_token in the auth object that gets returned
def prepare_flow(secrets_path, scope):
    # Grab settings from the client_secrets.json file provided by Google
    with open(secrets_path, 'r') as fp:
        obj = json.load(fp)

    # The secrets we need are in the 'web' node
    secrets = obj['web']

    # Return a Flow that requests a refresh_token
    return OAuth2WebServerFlow(
        client_id=secrets['client_id'],
        client_secret=secrets['client_secret'],
        scope=scope,
        redirect_uri=secrets['redirect_uris'][0],
        access_type='offline',
        approval_prompt='force')

您可以将对 flow_from_clientsecrets 的调用替换为对上述函数的调用:

flow = prepare_flow(client_secrets, scope)

我刚刚使用此Flow运行了一次干净的身份验证,并验证了从 Google 返回的身份验证负载中是否存在刷新 token 。

关于google-api-python-client - 使用 flow_from_clientsecrets() 指定 access_type=online,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26410957/

相关文章:

python - 如何在给定 OAuth2 access_token 的情况下授权 Python Google API 客户端服务?

python - 如何访问 Admin SDK 中的目录 API

python - client_secrets.json 文件部署到 GAE 后将无法读取

python - 从 Google Drive Api (python) 获取文件大小

python - 博主API : How to get posts *updated* in a particular timeframe?

python - 未使用 Google Drive API 获取共享文件夹中的项目列表

python - 如何使用 gmali-api 获取已读和未读或发送和接收的邮件数量?

python - 如何使用 API 或 python 包获取所有 azure vm 实例的配置

python - "Insufficient Permission: Request had insufficient authentication scopes"即使是最通用的范围

python - Google 和 Oauthlib - 范围已更改