【发布时间】:2021-10-20 17:27:00
【问题描述】:
我需要一些有关 Google OAuth 2.0 Playground 的帮助,希望有人能提供帮助。 我想知道如何通过 python 对 Google OAuth 2.0 Playground 的 HTTP 请求获取授权码?可能吗? 我正在尝试获取执行此请求的授权代码:
我的主要代码:
auth_url = "https://accounts.google.com/o/oauth2/auth"
access_token_url = "https://accounts.google.com/o/oauth2/token"
callback_url = "https://developers.google.com/oauthplayground"
client_id = 'xxx'
client_secret = xxx'
authorization_redirect_url = auth_url + '?response_type=code&client_id=' + client_id + '&redirect_uri=' + callback_url + '&scope=openid'
authorization_code = requests.get(authorization_redirect_url)
data = {'grant_type': 'authorization_code', 'code': authorization_code, 'redirect_uri': callback_url}
access_token_response = requests.post(access_token_url, data=data, verify=False, allow_redirects=False, auth=(client_id, client_secret))
如果我尝试运行我的代码,我会得到:
response:
{'Date': 'Wed, 18 Aug 2021 23:35:42 GMT', 'Expires': 'Mon, 01 Jan 1990 00:00:00 GMT', 'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate', 'Pragma': 'no-cache', 'Content-Type': 'application/json; charset=utf-8', 'Vary': 'Origin, X-Origin, Referer', 'Content-Encoding': 'gzip', 'Server': 'scaffolding on HTTPServer2', 'X-XSS-Protection': '0', 'X-Frame-Options': 'SAMEORIGIN', 'X-Content-Type-Options': 'nosniff', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"', 'Transfer-Encoding': 'chunked'}
body: {
"error": "invalid_grant",
"error_description": "Malformed auth code."
}
有什么建议吗?
亲切的问候, 朱利亚诺
【问题讨论】:
-
要获取代码,您需要启动一个网络服务器来响应redirect_url 回调。我写了一篇文章,解释了 curl 中的过程,包括一个特殊的 python web 服务器来响应 OAuth 回调:jhanley.com/google-oauth-2-0-testing-with-curl-version-2
-
嗨@JohnHanley,我会读你的文章。谢谢
标签: python google-cloud-platform google-cloud-pubsub oauth2-playground