我有一个已批准作为具有 openid
和 email
范围的授权 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/