gpt4 book ai didi

python - 如何在没有 python oauth2client lib 的情况下实现谷歌登录混合服务器端流程

转载 作者:行者123 更新时间:2023-12-01 07:28:07 25 4
gpt4 key购买 nike

我已经使用 python27 实现了混合服务器端 google 登录流程。其中webapp向后端发送一次代码,后端替换为refresh_token、id_token和access_token。我已按照此文档来实现此 https://developers.google.com/identity/sign-in/web/server-side-flow

但看起来 oauth2client 已被弃用。并且根据已弃用的注释,google-auth 没有任何方法来实现一次性代码身份验证流程。

下面的代码正在处理一次性 auth_code 并替换为 token 。对于没有 oauth2client 的 python 2.7 和 3.7 来说,最好的方法是什么?

from apiclient import discovery
import httplib2
from oauth2client import client

CLIENT_SECRET_FILE = '/path/to/client_secret.json'

# Exchange auth code for access token, refresh token, and ID token
credentials = client.credentials_from_clientsecrets_and_code(
CLIENT_SECRET_FILE,
['profile', 'email'],
auth_code)

最佳答案

  • 您希望在没有 oauth2client 的情况下实现您的脚本。

如果我的理解是正确的,那么这个修改怎么样?在此修改中,我使用了 google_auth_oauthlib。请将此视为多个答案之一。

修改后的脚本:

from google_auth_oauthlib.flow import Flow
from googleapiclient.discovery import build

# Create the flow using the client secrets file from the Google API Console.
flow = Flow.from_client_secrets_file(
'client_secret.json',
scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'],
redirect_uri='urn:ietf:wg:oauth:2.0:oob')

# Tell the user to go to the authorization URL.
auth_url, _ = flow.authorization_url(prompt='consent')

print('Please go to this URL: {}'.format(auth_url))

# The user will get an authorization code. This code is used to get the access token.
code = input('Enter the authorization code: ')
flow.fetch_token(code=code)
credentials = flow.credentials

# Retrieve file list using Drive API. This is a sample.
service = build('drive', 'v3', credentials=credentials)
files = service.files().list(pageSize=5).execute()
print(files)
  • 在此示例脚本中,运行该脚本时,用于检索授权代码的 URL 将显示在控制台上。当您获取代码并将其放入控制台时,当启用Drive API时,可以检索文件列表。

引用:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

关于python - 如何在没有 python oauth2client lib 的情况下实现谷歌登录混合服务器端流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com