gpt4 book ai didi

python - Google oAuth2 python 应用程序无法在线打开登录页面

转载 作者:行者123 更新时间:2023-12-05 05:35:56 27 4
gpt4 key购买 nike

我关注了this document创建 oAuth2 登录应用程序。

创建了一个项目并创建了一个 oAuth2 客户端 ID 作为 Web 应用程序。

enter image description here

可以看到下面的代码和指南中的是一样的。它在本地机器上运行良好。打开网络浏览器进行身份验证,但是当我将其上传到我的主机时。它不会打开任何东西。我上传到主机的路径是:

public_html/cgi-bin

并以 python3 quickstart.py 的形式运行脚本

快速启动.py

from __future__ import print_function
import os.path

from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']


def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())

try:
# Call the Gmail API
service = build('gmail', 'v1', credentials=creds)
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])

if not labels:
print('No labels found.')
return
print('Labels:')
for label in labels:
print(label['name'])

except HttpError as error:
# TODO(developer) - Handle errors from gmail API.
print('An error occurred: {error}')


if __name__ == '__main__':
main()

最佳答案

我手头没有 Gmail 网络示例。这是我的驱动器网络示例中的相关 block 。问题在于您使用的流程。

@app.route('/authorize')
def authorize():
# Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scopes=SCOPES)

# The URI created here must exactly match one of the authorized redirect URIs
# for the OAuth 2.0 client, which you configured in the API Console. If this
# value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch'
# error.
flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

authorization_url, state = flow.authorization_url(
# Enable offline access so that you can refresh an access token without
# re-prompting the user for permission. Recommended for web server apps.
access_type='offline')

# Store the state so the callback can verify the auth server response.
flask.session['state'] = state

return flask.redirect(authorization_url)


@app.route('/oauth2callback')
def oauth2callback():
# Specify the state when creating the flow in the callback so that it can
# verified in the authorization server response.
state = flask.session['state']

flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

# Use the authorization server's response to fetch the OAuth 2.0 tokens.
authorization_response = flask.request.url
flow.fetch_token(authorization_response=authorization_response)

# Store credentials in the session.
# ACTION ITEM: In a production app, you likely want to save these
# credentials in a persistent database instead.
credentials = flow.credentials
flask.session['credentials'] = credentials_to_dict(credentials)

return flask.redirect(flask.url_for('test_api_request'))

如果您无法理解,请告诉我,我可以帮您整理一个 Gmail 示例。

关于python - Google oAuth2 python 应用程序无法在线打开登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73388756/

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