Search code examples
pythondjangoauthenticationdjango-viewsdjango-allauth

"MultipleObjectsReturned or ObjectDoesNotExist error when accessing 'google' provider in Django-allauth"


I'm encountering an issue when trying to access the 'google' provider in Django-allauth. I'm getting either a MultipleObjectsReturned or ObjectDoesNotExist exception. I have followed the documentation and tried various troubleshooting steps, but the problem persists.

Here is the code snippet from my views.py file:

from allauth.socialaccount.models import SocialApp
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist

def home(request):
    try:
        google_provider = SocialApp.objects.get(provider='google')
    except MultipleObjectsReturned:
        print("Multiple 'google' providers found:")
        for provider in SocialApp.objects.filter(provider='google'):
            print(provider.client_id)
        raise
    except ObjectDoesNotExist:
        print("No 'google' provider found.")
        raise

    return render(request, 'home.html', {'google_provider': google_provider})

I have also imported the necessary modules SocialApp and MultipleObjectsReturned/ObjectDoesNotExist from allauth.socialaccount.models and django.core.exceptions respectively.

I have checked my database, and there is only one entry for the 'google' provider in the SocialApp table. The client_id is correctly set for this provider.

Despite taking these steps, I am still encountering the mentioned errors. I have made sure to restart my Django server after any code or database changes.


Solution

  • you probably have 2 instance of of your google account set up. one in your settings.py and the other one in your django admin console.

    best you delete anyone you think you should delete and make migrations and migrate

    #settings.py (you could delete this and leave the one in the admin)

    SOCIALACCOUNT_PROVIDERS = {
        'google': {
            # For each OAuth based provider, either add a ``SocialApp``
            # (``socialaccount`` app) containing the required client
            # credentials, or list them here:
            'APP': {
                'client_id': '123',
                'secret': '456',
                'key': ''
            }
        }
    }