4

我正在尝试让我的 google 身份验证在请求 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 身份验证错误消息的照片


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())
4

3 回答 3

2

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

转到您的 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状态

在此处输入图像描述

于 2021-10-21T03:16:42.197 回答
0

检查您是否登录到您的谷歌帐户。

我使用的是 google chrome 浏览器,结果我在会话过期时退出了 Gmail,当我登录到 Gmail 时,问题得到了解决

于 2021-12-07T18:26:48.947 回答
0

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

于 2021-09-03T01:52:30.970 回答