如何在没有 python oauth2client 库的情况下实现 google 登录混合服务器端流程
How to implement google signin hybrid server-side flow without python oauth2client lib
我已经使用 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 并替换为令牌。对于没有 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 将显示到控制台。当您获得代码并将其放入控制台时,当启用驱动器 API 时,可以检索文件列表。
参考:
如果我误解了你的问题,这不是你想要的方向,我深表歉意。
我已经使用 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 并替换为令牌。对于没有 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 将显示到控制台。当您获得代码并将其放入控制台时,当启用驱动器 API 时,可以检索文件列表。
参考:
如果我误解了你的问题,这不是你想要的方向,我深表歉意。