Google OAuth2 -Anmeldung in Django: Fehler beim ersten Anmeldeversuch "Schlechte Anfrage"Python

Python-Programme
Anonymous
 Google OAuth2 -Anmeldung in Django: Fehler beim ersten Anmeldeversuch "Schlechte Anfrage"

Post by Anonymous »

Ich arbeite an der Integration von Google OAuth2 -Login/Anmeldung mit Django, aber ich stimme während des ersten Anmeldungsversuchs auf ein Problem. Mit Google "Taste. : Dies geschieht erst zum ersten Mal, wenn sich der Benutzer anmelden versucht. Es gibt einen Fehler "schlechter Anforderung" zurück. />
Relevanter Code:
backend/api/urls.py:

Code: Select all

path('auth/google/', GoogleLogin.as_view(), name='google_login')

backend/api/views.py:

Code: Select all

from django.conf import settings
from django.http import HttpResponseRedirect

from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client

from dj_rest_auth.registration.views import SocialLoginView

class GoogleLogin(SocialLoginView):
adapter_class = GoogleOAuth2Adapter
client_class = OAuth2Client
callback_uri = settings.REDIRECT_URI

def get(self, request):
return HttpResponseRedirect(redirect_to=settings.GOOGLE_AUTH_URL)

backend/api/adapter.py:

Code: Select all

from allauth.socialaccount.adapter import DefaultSocialAccountAdapter

from api.models import User

class CustomSocialAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
# Ignore existing social accounts, just do this stuff for new ones

if sociallogin.is_existing:
return

# some social logins don't have an email address, e.g. facebook accounts
# with mobile numbers only, but allauth takes care of this case so just
# ignore it

if 'email' not in sociallogin.account.extra_data:
return

# check if given email address already exists.
# Note: __iexact is used to ignore cases

try:
email = sociallogin.account.extra_data['email'].lower()
existing_user = User.objects.get(email__iexact=email)

# if it does not, let allauth take care of this new social account
except User.DoesNotExist:
return

# if it does, connect this new social login to the existing user
sociallogin.connect(request, existing_user)

Jede Hilfe wäre sehr geschätzt!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post