大佬教程收集整理的这篇文章主要介绍了Google API OAuth 2.0 无法验证错误 400:redirect_uri_mismatch,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个对多个用户日历事件具有只读访问权限的应用程序。但是,我经常无法通过身份验证步骤。浏览器打开带有错误 400:redirect_uri_mismatch 的新同意屏幕。这是我所做的:
为此,我在谷歌控制台中设置了我的项目。@R_3_10673@下:
{"web":
{
"clIEnt_ID":"123234-jjjjj.apps.Googleusercontent.com","project_ID":"ppppp-1234","auth_uri":"https://accounts.Google.com/o/oauth2/auth","token_uri":"https://oauth2.GoogleAPIs.com/token","auth_provIDer_x509_cert_url":"https://www.GoogleAPIs.com/oauth2/v1/certs","clIEnt_secret":"zzzzzzzzzzzzzzzzzz","redirect_uris":["http://localhost:8080"],"JavaScript_origins":["http://localhost"]
}
}
我使用以下代码调用 Google Api:
import pickle
import os
from Google_auth_oauthlib.flow import Flow,InstalledAppFlow
from Googleapiclient.discovery import build
from Googleapiclient.http import MediafileUpload
from Google.auth.transport.requests import request
def Create_service(clIEnt_secret_file,API_name,API_version,*scopes):
CLIENT_SECRET_file = clIEnt_secret_file
API_service_name = API_name
API_VERSION = API_version
ScopES = [scope for scope in scopes[0]]
cred = None
pickle_file = f'token_{API_service_name}_{API_VERSION}.pickle'
if os.path.exists(pickle_filE):
with open(pickle_file,'rb') as token:
cred = pickle.load(token)
if not cred or not cred.valID:
if cred and cred.expired and cred.refresh_token:
cred.refresh(request())
else:
flow = InstalledAppFlow.from_clIEnt_secrets_file(CLIENT_SECRET_file,ScopES)
cred = flow.run_local_server()
with open(pickle_file,'wb') as token:
pickle.dump(cred,token)
try:
service = build(API_service_name,API_VERSION,credentials=cred)
print(API_service_name,'service created successfully')
return service
except Exception as e:
print(E)
return None
CLIENT_SECRET_file = 'token.Json'
API_name = 'calendar'
API_VERSION = 'v3'
ScopES = ['https://www.GoogleAPIs.com/auth/calendar.events.Readonly']
service = Create_service(CLIENT_SECRET_file,API_name,ScopES)
colors = service.colors().get().execute()
print(colors)
当我运行它时,浏览器打开一个同意屏幕
错误 400:redirect_uri_mismatch。 然后我访问 http://localhost:8080/ 并看到消息“身份验证流程已完成。您可以关闭此窗口。”
@L_403_0@
在控制台中,我看到以下错误:
file "main.py",line 48,in <module>
service = Create_service(CLIENT_SECRET_file,ScopES)
file "main.py",line 29,in Create_service
cred = flow.run_local_server()
file "/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/Google_auth_oauthlib/flow.py",line 480,in run_local_server
self.fetch_token(authorization_response=authorization_responsE)
file "/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/Google_auth_oauthlib/flow.py",line 288,in fetch_token
return self.oauth2session.fetch_token(self.clIEnt_config["token_uri"],**kwargs)
file "/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/requests_oauthlib/oauth2_session.py",line 239,in fetch_token
self._clIEnt.parse_request_uri_response(
file "/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/clIEnts/web_application.py",line 203,in parse_request_uri_response
response = parse_authorization_code_response(uri,state=statE)
file "/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/parameters.py",line 268,in parse_authorization_code_response
raise MismatchingStateError()
oauthlib.oauth2.rfc6749.errors.MismatchingStateError: (mismatching_statE) CSRF Warning! State not equal in request and response.
那么,问题是如何以正确的方式通过 OAuth 2.0 对 Google Api 进行身份验证?
redirect_uri_mismatch
Web 应用程序使用重定向 uri 来表示您的应用程序准备接受来自谷歌授权服务器的授权响应的位置。它基本上是 Web 服务器上准备响应授权的 URL。重定向 uri 仅用于 Web 应用程序。
在您的情况下,您正在创建一个已安装的应用程序 InstalledAppFlow
,在这种情况下,您应该已在 Google 开发者控制台上创建了已安装的(桌面)凭据。桌面客户端能够从他们发送的位置接受授权响应,通常是本地主机,因此授权服务器只需将令牌返回到调用的来源。
由于您的代码旨在通过向其提供 Web 应用程序凭据来作为已安装的应用程序运行,因此会导致它失败,因为该应用程序未发送正确的请求。
我在这里有一个视频,它将向您展示如何在 google 开发者控制台上创建 How to create Google Oauth2 installed application credentials。
以上是大佬教程为你收集整理的Google API OAuth 2.0 无法验证错误 400:redirect_uri_mismatch全部内容,希望文章能够帮你解决Google API OAuth 2.0 无法验证错误 400:redirect_uri_mismatch所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。