问题:缺少OAuth 2刷新令牌.
问题是localhost版本接收Refresh Token授权令牌的一部分,但GCE中运行的相同代码不会.
细节:
我编写了一个实现Google OAuth 2.0的Python Flask应用程序.此Web应用程序在云中运行,具有经过验证的域名,有效的SSL证书和HTTPS端点.此未修改的Web应用程序也以localhost.运行时之间的区别在于localhost版本不使用TLS.代码流中没有其他差异.
除了Refresh Token缺少,我不能自动更新token,一切都很完美.
我已经广泛研究过这个问题.access_type=offline正确实现API等问题,否则我不会Refresh Token在localhost版本中获得.
我正在使用requests_oauthlibpython库.
gcp = OAuth2Session(
app.config['gcp_client_id'],
scope=scope,
redirect_uri=redirect_uri)
# print('Requesting authorization url:', authorization_base_url)
authorization_url, state = gcp.authorization_url(
authorization_base_url,
access_type="offline",
prompt="select_account",
include_granted_scopes='true')
session['oauth_state'] = state
return redirect(authorization_url)
# Next section of code after the browser approves the request
token = gcp.fetch_token(
token_url,
client_secret=app.config['gcp_client_secret'],
authorization_response=request.url)
Run Code Online (Sandbox Code Playgroud)
令牌refresh_token在运行时有,localhost但在云中运行时没有.
此Google文档讨论了刷新令牌,表明Web应用程序支持此功能.
[更新11/18/2018]
我发现这个 …
google-openid oauth-2.0 google-oauth google-cloud-platform requests-oauthlib
运行以下命令时(example.com显然,用我们的 API 替换)
req = Request('GET', 'https://example.com')
# client is a customized OAuth2Session
client.authorize(self.username, self.password, self.auth_key)
print(self.client.authorized) # True
Run Code Online (Sandbox Code Playgroud)
返回值如下<Response [200]>:
response = client.request(req.method, req.url)
Run Code Online (Sandbox Code Playgroud)
但这返回<Response [401]>:
prepped = client.prepare_request(req)
response = client.send(prepped)
Run Code Online (Sandbox Code Playgroud)
Request通过 发送时如何重用原始对象OAuth2Session?
python oauth-2.0 python-3.x python-requests requests-oauthlib
我正在使用 authlib https://github.com/lepture/authlib获取用户对其数据的身份验证,因此每日离线调度程序可以代表用户下载一些数据。
我首先注册客户端:
google = oauth.register(
'google',
client_id = '***',
client_secret = '***',
access_token_url = "https://www.googleapis.com/oauth2/v4/token",
access_token_params = None,
authorize_url = "https://accounts.google.com/o/oauth2/v2/auth",
authorize_params = None,
api_base_url = 'https://googleapis.com/oauth2/v1/',
client_kwargs={'scope': 'https://www.googleapis.com/auth/doubleclickbidmanager'}
)
Run Code Online (Sandbox Code Playgroud)
在稍后阶段,我使用以下方法检索令牌:
token = oauth.google.authorize_access_token()
Run Code Online (Sandbox Code Playgroud)
当我打印令牌时,我看到 Google 没有返回我需要存储在数据库中以供离线使用的刷新令牌:
{'access_token': '***', 'expires_in': 3599, 'scope': 'https://www.googleapis.com/auth/doubleclickbidmanager', 'token_type': 'Bearer', 'expires_at': 1591750317}
Run Code Online (Sandbox Code Playgroud)
我可以更改使用 access_type = offline 注册客户端的方式,以便让 Google 知道我也需要刷新令牌吗?如何获取和存储刷新令牌?