Google oAuth 2.0 API 身份验证错误:错误 400 - redirect_uri_mismatch(不符合策略)DJANGO APP

新手上路,请多包涵

我正在尝试让我的谷歌身份验证在请求 Gmail 和日历数据的 Django 应用程序上工作。我已经在 Google 开发人员控制台中设置了 oAuth API 并将其与我的项目相关联,并且我已经三重检查了我的重定向 URI 是否与代码中的完全匹配(HTTP 与 HTTPS 没有错误,斜杠没有任何不一致) ).我确保我的密钥、密钥、ClientID 和客户端密钥在我的 Django 应用程序的管理页面中都已配置且完全相同。我已经关注了许多 youtube 教程并搜索了有关堆栈溢出的其他问题,但身份验证仍然无法正常工作。我收到错误 400:redirect_uri_mismatch。尽管我已经检查了很多次以确认它们是相同的。

从所有教程中,我了解到此错误有两个主要来源:

  1. 服务器端(可以在云托管开发者控制台中修复)
  2. 客户端(可以通过更改代码来修复)

这两个错误都有自己的个性化消息,说明它是什么类型的不匹配。

但是,我的是这样说的:您无法登录此应用,因为它不符合 Google 的 OAuth 2.0 政策。 \n\n如果您是应用开发者,请在 Google Cloud Console 中注册重定向 URI。

这是错误的照片 [![Google 身份验证错误消息][1]][1]


from django.shortcuts import render, redirect
from django.http import HttpRequest
from google_auth_oauthlib.flow import Flow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from .models import CredentialsModel
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
import os

os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
#Scopes are what we should be allowed to access
SCOPES = ['https://mail.google.com/', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'openid']

"""
IF HAVING ISSUES WITH ANON USER:
Make sure you are on 127.0.0.1:8000, not localhost, both from the test-page and
the callback page. For some reason they are treated as different sessions and thus will have
issues maintaining a logged in user
"""

def oauth2callback(request):
    activeUser = request.user
    #URL is what we need to use for authentication
    authorization_response = request.build_absolute_uri()
    flow = Flow.from_client_secrets_file(
            settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
            scopes=SCOPES,

            #This is where we are redirected after authentication
            redirect_uri='http://127.0.0.1:8000/google/oauth2callback')
    #Now get proper token
    flow.fetch_token(authorization_response = authorization_response)
    #print(request.user)
    #Now save in our database
    #print(flow.credentials)
    try :
        my_credential = CredentialsModel.objects.get(pk = activeUser)
    except ObjectDoesNotExist:
        CredentialsModel.objects.create(id = activeUser, credential = flow.credentials)
    else:
        my_credential.credential = flow.credentials
        my_credential.save()

    return redirect(flow.redirect_uri)     #activeUser.get_absolute_url())

  [1]: https://i.stack.imgur.com/2HXGP.png

原文由 Retdude 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.8k
1 个回答

谷歌的文档在这部分并不清楚(也可能是谷歌端的错误):

go to your GCP console, under OAuth consent screen , when the Publishing status is In production , we can still put http://localhost:8080/oauth-authorized/google under the Authorized redirect URIs 没有触发红色错误消息 Invalid Redirect但是,除非应用程序处于 Testing 状态,否则它不起作用。

在此处输入图像描述

因此,为了在 http://127.0.0.1:8000 测试您的应用程序,您需要将您的 GCP 应用程序设置为 Testing 状态

在此处输入图像描述

原文由 Zach 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏