[英]redirect uri mismatch in fastapi
I hope everyone is fine.我希望每个人都很好。 I am trying to implement google sso on my fastapi app.
我正在尝试在我的 fastapi 应用程序上实施 google sso。 after entering the user credentials is entered and it gets redirected while redirecting i am getting this error
输入用户凭据后,它在重定向时被重定向,我收到此错误
google_sso = GoogleSSO("client-id", "client-secret", "http://127.0.0.1:8000/google/callback/")
@app1.get("/google/login")
async def google_login():
"""Generate login url and redirect"""
return await google_sso.get_login_redirect()
@app1.get("/google/callback")
async def google_callback(request: Request):
"""Process login response from Google and return user info"""
user = await google_sso.verify_and_process(request)
print("Hellooooooooooooooo")
print(user, "11111111111111")
return {
"id": user.id,
"picture": user.picture,
"display_name": user.display_name,
"email": user.email,
"provider": user.provider,
}
I have shared the URL configuration in google dashboard in below screenshot我在下面的屏幕截图中分享了谷歌仪表板中的 URL 配置
enter image description here在此处输入图像描述
the error i have mentioned below我在下面提到的错误
oauthlib.oauth2.rfc6749.errors.CustomOAuth2Error: (redirect_uri_mismatch) Bad Request
The problem could lay in the process_login() function which is getting called in the verify_and_process() function in your /callback api.问题可能在于 process_login() function,它在您的 /callback api 中的 verify_and_process() function 中被调用。
Let's take a look inside the process_login() function ( https://tomasvotava.github.io/fastapi-sso/sso/base.html#fastapi_sso.sso.base.SSOBase.verify_and_process ):让我们来看看 process_login() function ( https://tomasvotava.github.io/fastapi-sso/sso/base.html#fastapi_sso.sso.base.SSOBase.verify_and_process ):
async def process_login(self, code: str, request: Request) -> Optional[OpenID]:
"""This method should be called from callback endpoint to verify the user and request user info endpoint.
This is low level, you should use {verify_and_process} instead.
"""
url = request.url
current_url = str(url).replace("http://", "https://")
current_path = f"https://{url.netloc}{url.path}"
I guess the (redirect_uri_mismatch) error is because you are using a HTTP redirect_url in your GoogleSSO() call:我猜 (redirect_uri_mismatch) 错误是因为您在 GoogleSSO() 调用中使用了 HTTP redirect_url:
google_sso = GoogleSSO("client-id", "client-secret", "http://127.0.0.1:8000/google/callback/")
Inside the process_login() function the HTTP of the redirect url inside the url of your request is replaced with HTTPS:在 process_login() function 中,重定向 url 中的 HTTP 在您请求的 url 中被替换为 HTTPS:
url = request.url
current_url = str(url).replace("http://", "https://")
After that replacement you have a mismatch with your redirect url, because替换后,您的重定向 url 不匹配,因为
https://127.0.0.1:8000/google/callback/
is not
http://127.0.0.1:8000/google/callback/
They are two different urls.它们是两个不同的网址。
Solution could be that you secure your server with HTTPS via self signed certificates.解决方案可能是您通过自签名证书使用 HTTPS 保护您的服务器。 (That one is very simple: https://dev.to/rajshirolkar/fastapi-over-https-for-development-on-windows-2p7d )
(那个很简单: https://dev.to/rajshirolkar/fastapi-over-https-for-development-on-windows-2p7d )
Btw.顺便提一句。 did you register you app in the google cloud ( https://developers.google.com/identity/sign-in/web/sign-in )?
您是否在谷歌云中注册了您的应用程序( https://developers.google.com/identity/sign-in/web/sign-in )? Because you are using "client-id" and "client-secret" as parameters.
因为您使用“client-id”和“client-secret”作为参数。
or要么
This is because the port number is changing in the redirect URI, everytime you run the application.这是因为每次运行应用程序时,重定向 URI 中的端口号都会发生变化。 So everytime you run it it becomes:
所以每次你运行它都会变成:
http://localhost:65280/
http://localhost:65230/
http://localhost:63280/
And so forth.等等。 I dont have a solution for you yet.
我还没有适合你的解决方案。 Working on it myself right now.
现在我自己在做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.