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

Ret*_*ude 14 python django google-authenticator google-oauth google-api-python-client

我正在尝试让我的谷歌身份验证在请求 Gmail 和日历数据的 Django 应用程序上运行。我已经在 Google 开发者控制台中设置了 oAuth API 并将其与我的项目链接,并且我已经三次检查我的重定向 URI 是否与代码中的完全匹配(HTTP 与 HTTPS 没有错误,斜杠也没有任何不一致) )。我确保我的密钥、密钥、ClientID 和 Client Secret 在 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
Run Code Online (Sandbox Code Playgroud)

Zac*_*ach 10

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

转到您的 GCP 控制台,在 下OAuth consent screen,当Publishing status是 时In production,我们仍然可以将 放在http://localhost:8080/oauth-authorized/googleAuthorized redirect URIs,而不会触发红色错误消息Invalid Redirect但是,除非应用程序处于状态,否则它不起作用Testing

在此输入图像描述

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

在此输入图像描述


Moh*_*ady 1

嘿,我正在 ASP.Net MVC 中处理这个问题,我认为原因在 php 中是相同的,但无论如何,请确保将下面 img 中的 url 复制到 Google 云控制台中 OAuth 2.0 客户端 ID 中的授权重定向 URI。