python - 如何通过 HTTP 请求获取 Google OAuth 2.0 Playground 的授权码 - Python
问题描述
我需要一些有关 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."
}
有什么建议吗?
亲切的问候,朱利亚诺
解决方案
抱歉,如果我错过了您的意图;不清楚为什么要使用 OAuth Playground 自动化流程。
如果您尝试为 Google 的任何服务自动化 OAuth 流程,我鼓励您使用 Google 的 (Python) SDK,而不是手动滚动您自己的身份验证代码。
例如,请参阅此处为(该)Gmail 服务记录的 OAuth 流程。
Google API 的一个优势在于,每项Google 服务都有一个 API,一旦您熟悉了一项服务的机制,您就可以将这些原则应用于每项服务。
推荐阅读
- c++ - Select object in OpenGL when doing transformations in the vertex shader
- python - Heron TumblingWindowBolt 基于计数而不是秒
- autodesk-forge - 互连 Autodesk forge 和 Vault api
- mysql - MariaDB 服务器的运行速度比我们可以重现的任何东西快 10 倍。想法为什么?
- sql - DQS 和 Azure SQL
- php - 提交数据 Laravel 后的自定义错误消息
- kubernetes - 如何解决 Istio 流量路由错误?
- regex - 带有迭代器的正则表达式
- r - facet_zoom 不能改变缩放图的中断
- python - Python在读取文件后删除文件中的行