Python Django应用遇Google OAuth 2.0 redirect_uri_mismatch问题
谷歌OAuth 400 redirect_uri_mismatch 疑难问题求助
我翻遍了StackOverflow上的相关帖子,试过所有常见解决方案,还是卡在400 - redirect_uri_mismatch错误——严格按照指引操作也没用。
核心情况速览
- 技术栈:Python + Django Web应用
- OAuth客户端类型:
web application,未发布 - 已在Credentials中添加带/不带尾斜杠的Redirect URI
- 使用通用
Flow+web关键字,未使用InstalledAppFlow - Redirect URI可正常加载响应
- 仍触发
redirect_uri_mismatch错误
详细信息
- 应用注册为“Web应用”,处于
Testing模式(附截图) - Cloud Console中已添加包含
127/localhost、带/不带尾斜杠的所有URI变体(附截图) - 代码配置如下,生成的请求URI与配置完全一致,但仍报错:
REDIRECT_URI = "http://localhost:8000/dev/test/onboard_finalize" CLIENT_SECRETS = { "web": { "client_id": {CLIENT_ID}, "client_secret": {CLIENT_SECRET}, "redirect_uris": [REDIRECT_URI], "project_id": {PROJECT_ID}, "auth_provider_x509_cert_url": AUTH_URL, "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token" } } sesh = OAuth2Session(CLIENT_ID, scope=SCOPES, redirect_uri=REDIRECT_URI) flow = Flow( oauth2session = sesh, client_type = "web", client_config = CLIENT_SECRETS, redirect_uri = REDIRECT_URI, code_verifier = self.state, ) url, state = flow.authorization_url(prompt="select_account") print("Using REDIRECT_URI:", REDIRECT_URI) print("Generated URL:", url)
执行输出:
Using REDIRECT_URI: http://localhost:8000/dev/test/onboard_finalize Generated URL: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=****.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fdev%2Ftest%2Fonboard_finalize&scope=openid+profile&state=lVtLKgPPtyhjjpll5Wd8KEnZjSqGty&code_challenge=****&code_challenge_method=S256&prompt=select_account&access_type=offline
错误详情显示请求URI与配置一致,但仍报错(附截图)。用oauthplayground测试也出现相同错误,所有配置的URI都触发问题。
4. 已验证Redirect URI可正常访问(附截图)
内容的提问来源于stack exchange,提问作者Vegard
相关产品推荐
相关产品推荐

