如何通过 Google OAuth 2.0 游乐场的 HTTP 请求获取授权码 - Python

How to get an authorization code via HTTP request for Google OAuth 2.0 Playground - Python

我在 Google OAuth 2.0 游乐场方面需要一些帮助,希望有人能提供帮助。 我想知道如何通过 Google OAuth 2.0 Playground python 的 HTTP 请求获取授权码?可能吗? 我正在尝试获取执行此请求的授权码:

我试图转发此 post 预期的授权码:

我的主要代码:

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 而不是手动-roll 你自己的授权码。

例如,请参阅 here Gmail 服务的 OAuth 流程记录。

Google API 的优势在于,每个 Google 服务都有一个 API,一旦您熟悉了一项服务的机制, 您可以将这些原则应用于每项服务。