social (Google) OAuth 2.0 authentication

Currently developing a django app, and I used social (Google) OAuth 2.0 authentication.

I deleted the app from my local computer and git cloned it. Google Sign-in was working fine, I deleted the app for something else.

Anyways, after I cloned it from github, when when I tried “sign in with google”, it says “Exception Value: Reverse for 'google_login' not found. 'google_login' is not a valid view function or pattern name.”, it’s also highlighting <a href="{% provider_login_url 'google'%}?next=/">.

I don’t think that I need to I define the url, because I installed django-allauth, and it should come with that. I am just so confused why would it work before and after I git cloned it, it stop working. Couldn’t find out the reason online.

Any idea? Thanks!

The setup steps include creating some data in the admin. Did you redo that step as well?

Yup. The info was still in there under social applications.

Can you install django-extensions and run python manage.py show_urls, then copy/paste the output here? I’m not super familiar with allauth, but I think the urls should get registered in there.

Can you share your INSTALLED_APPS setting too please?

Yes.

**settings.py**
INSTALLED_APPS = [
    'main_app',
    'widget_tweaks',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
]

SOCIALACCOUNT_LOGIN_ON_GET=True

AUTHENTICATION_BACKENDS = [
    'allauth.account.auth_backends.AuthenticationBackend'
]
SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        }
    }
}

STATIC_URL = 'static/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

**url.py**
urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
    path('social/signup/', views.signup_redirect, name='signup_redirect'), 
    path('', include('allauth.urls')),
    path('', include('main_app.urls')),
]

Sorry, I missed this message earlier.

/       main_app.views.home     home
/accounts/login/        django.contrib.auth.views.LoginView     login
/accounts/logout/       django.contrib.auth.views.LogoutView    logout
/accounts/password_change/      django.contrib.auth.views.PasswordChangeView    password_change
/accounts/password_change/done/ django.contrib.auth.views.PasswordChangeDoneView        password_change_done
/accounts/password_reset/       django.contrib.auth.views.PasswordResetView     password_reset
/accounts/password_reset/done/  django.contrib.auth.views.PasswordResetDoneView password_reset_done
/accounts/reset/<uidb64>/<token>/       django.contrib.auth.views.PasswordResetConfirmView      password_reset_confirm
/accounts/reset/done/   django.contrib.auth.views.PasswordResetCompleteView     password_reset_complete
/accounts/signup/       main_app.views.signup   signup
/admin/ django.contrib.admin.sites.index        admin:index
/admin/<app_label>/     django.contrib.admin.sites.app_index    admin:app_list
/admin/<url>    django.contrib.admin.sites.catch_all_view
/admin/account/emailaddress/    django.contrib.admin.options.changelist_view    admin:account_emailaddress_changelist
/admin/account/emailaddress/<path:object_id>/   django.views.generic.base.RedirectView
/admin/account/emailaddress/<path:object_id>/change/    django.contrib.admin.options.change_view        admin:account_emailaddress_change
/admin/account/emailaddress/<path:object_id>/delete/    django.contrib.admin.options.delete_view        admin:account_emailaddress_delete
/admin/account/emailaddress/<path:object_id>/history/   django.contrib.admin.options.history_view       admin:account_emailaddress_history
/admin/account/emailaddress/add/        django.contrib.admin.options.add_view   admin:account_emailaddress_add
/admin/auth/group/      django.contrib.admin.options.changelist_view    admin:auth_group_changelist
/admin/auth/group/<path:object_id>/     django.views.generic.base.RedirectView
/admin/auth/group/<path:object_id>/change/      django.contrib.admin.options.change_view        admin:auth_group_change
/admin/auth/group/<path:object_id>/delete/      django.contrib.admin.options.delete_view        admin:auth_group_delete
/admin/auth/group/<path:object_id>/history/     django.contrib.admin.options.history_view       admin:auth_group_history
/admin/auth/group/add/  django.contrib.admin.options.add_view   admin:auth_group_add
/admin/auth/user/       django.contrib.admin.options.changelist_view    admin:auth_user_changelist
/admin/auth/user/<id>/password/ django.contrib.auth.admin.user_change_password  admin:auth_user_password_change
/admin/auth/user/<path:object_id>/      django.views.generic.base.RedirectView
/admin/auth/user/<path:object_id>/change/       django.contrib.admin.options.change_view        admin:auth_user_change
/admin/auth/user/<path:object_id>/delete/       django.contrib.admin.options.delete_view        admin:auth_user_delete
/admin/auth/user/<path:object_id>/history/      django.contrib.admin.options.history_view       admin:auth_user_history
/admin/auth/user/add/   django.contrib.auth.admin.add_view      admin:auth_user_add
/admin/autocomplete/    django.contrib.admin.sites.autocomplete_view    admin:autocomplete
/admin/jsi18n/  django.contrib.admin.sites.i18n_javascript      admin:jsi18n
/admin/login/   django.contrib.admin.sites.login        admin:login
/admin/logout/  django.contrib.admin.sites.logout       admin:logout
/admin/main_app/photo/  django.contrib.admin.options.changelist_view    admin:main_app_photo_changelist
/admin/main_app/photo/<path:object_id>/ django.views.generic.base.RedirectView
/admin/main_app/photo/<path:object_id>/change/  django.contrib.admin.options.change_view        admin:main_app_photo_change
/admin/main_app/photo/<path:object_id>/delete/  django.contrib.admin.options.delete_view        admin:main_app_photo_delete
/admin/main_app/photo/<path:object_id>/history/ django.contrib.admin.options.history_view       admin:main_app_photo_history
/admin/main_app/photo/add/      django.contrib.admin.options.add_view   admin:main_app_photo_add
/admin/main_app/post/   django.contrib.admin.options.changelist_view    admin:main_app_post_changelist
/admin/main_app/post/<path:object_id>/  django.views.generic.base.RedirectView
/admin/main_app/post/<path:object_id>/change/   django.contrib.admin.options.change_view        admin:main_app_post_change
/admin/main_app/post/<path:object_id>/delete/   django.contrib.admin.options.delete_view        admin:main_app_post_delete
/admin/main_app/post/<path:object_id>/history/  django.contrib.admin.options.history_view       admin:main_app_post_history
/admin/main_app/post/add/       django.contrib.admin.options.add_view   admin:main_app_post_add
/admin/password_change/ django.contrib.admin.sites.password_change      admin:password_change
/admin/password_change/done/    django.contrib.admin.sites.password_change_done admin:password_change_done
/admin/r/<int:content_type_id>/<path:object_id>/        django.contrib.contenttypes.views.shortcut      admin:view_on_site
/admin/sites/site/      django.contrib.admin.options.changelist_view    admin:sites_site_changelist
/admin/sites/site/<path:object_id>/     django.views.generic.base.RedirectView
/admin/sites/site/<path:object_id>/change/      django.contrib.admin.options.change_view        admin:sites_site_change
/admin/sites/site/<path:object_id>/delete/      django.contrib.admin.options.delete_view        admin:sites_site_delete
/admin/sites/site/<path:object_id>/history/     django.contrib.admin.options.history_view       admin:sites_site_history
/admin/sites/site/add/  django.contrib.admin.options.add_view   admin:sites_site_add
/admin/socialaccount/socialaccount/     django.contrib.admin.options.changelist_view    admin:socialaccount_socialaccount_changelist
/admin/socialaccount/socialaccount/<path:object_id>/    django.views.generic.base.RedirectView
/admin/socialaccount/socialaccount/<path:object_id>/change/     django.contrib.admin.options.change_view        admin:socialaccount_socialaccount_change
/admin/socialaccount/socialaccount/<path:object_id>/delete/     django.contrib.admin.options.delete_view        admin:socialaccount_socialaccount_delete
/admin/socialaccount/socialaccount/<path:object_id>/history/    django.contrib.admin.options.history_view       admin:socialaccount_socialaccount_history
/admin/socialaccount/socialaccount/add/ django.contrib.admin.options.add_view   admin:socialaccount_socialaccount_add
/admin/socialaccount/socialapp/ django.contrib.admin.options.changelist_view    admin:socialaccount_socialapp_changelist
/admin/socialaccount/socialapp/<path:object_id>/        django.views.generic.base.RedirectView
/admin/socialaccount/socialapp/<path:object_id>/change/ django.contrib.admin.options.change_view        admin:socialaccount_socialapp_change
/admin/socialaccount/socialapp/<path:object_id>/delete/ django.contrib.admin.options.delete_view        admin:socialaccount_socialapp_delete
/admin/socialaccount/socialapp/<path:object_id>/history/        django.contrib.admin.options.history_view       admin:socialaccount_socialapp_history
/admin/socialaccount/socialapp/add/     django.contrib.admin.options.add_view   admin:socialaccount_socialapp_add
/admin/socialaccount/socialtoken/       django.contrib.admin.options.changelist_view    admin:socialaccount_socialtoken_changelist
/admin/socialaccount/socialtoken/<path:object_id>/      django.views.generic.base.RedirectView
/admin/socialaccount/socialtoken/<path:object_id>/change/       django.contrib.admin.options.change_view        admin:socialaccount_socialtoken_change
/admin/socialaccount/socialtoken/<path:object_id>/delete/       django.contrib.admin.options.delete_view        admin:socialaccount_socialtoken_delete
/admin/socialaccount/socialtoken/<path:object_id>/history/      django.contrib.admin.options.history_view       admin:socialaccount_socialtoken_history
/admin/socialaccount/socialtoken/add/   django.contrib.admin.options.add_view   admin:socialaccount_socialtoken_add
/chatter/       main_app.views.posts_index      posts_index
/chatter/<int:pk>/delete/       main_app.views.PostDelete       post_delete
/chatter/<int:pk>/edit/ main_app.views.PostEdit post_edit
/chatter/<int:post_id>/ main_app.views.post_detail      post_detail
/chatter/<int:post_id>/add_photo        main_app.views.add_photo        add_photo
/chatter/<int:post_id>/delete_photo/<int:photo_id>/     main_app.views.delete_photo     delete_photo
/chatter/compose/       main_app.views.PostCompose      post_compose
/confirm-email/ allauth.account.views.EmailVerificationSentView account_email_verification_sent
/confirm-email/<key>/   allauth.account.views.ConfirmEmailView  account_confirm_email
/email/ allauth.account.views.EmailView account_email
/inactive/      allauth.account.views.AccountInactiveView       account_inactive
/login/ allauth.account.views.LoginView account_login
/logout/        allauth.account.views.LogoutView        account_logout
/password/change/       allauth.account.views.PasswordChangeView        account_change_password
/password/reset/        allauth.account.views.PasswordResetView account_reset_password
/password/reset/done/   allauth.account.views.PasswordResetDoneView     account_reset_password_done
/password/reset/key/<uidb36>-<key>/     allauth.account.views.PasswordResetFromKeyView  account_reset_password_from_key
/password/reset/key/done/       allauth.account.views.PasswordResetFromKeyDoneView      account_reset_password_from_key_done
/password/set/  allauth.account.views.PasswordSetView   account_set_password
/search/        main_app.views.search   search
/signup/        allauth.account.views.SignupView        account_signup
/social/connections/    allauth.socialaccount.views.ConnectionsView     socialaccount_connections
/social/login/cancelled/        allauth.socialaccount.views.LoginCancelledView  socialaccount_login_cancelled
/social/login/error/    allauth.socialaccount.views.LoginErrorView      socialaccount_login_error
/social/signup/ allauth.socialaccount.views.SignupView  socialaccount_signup
/social/signup/ main_app.views.signup_redirect  signup_redirect

Thank you so much for your help.

Odd. So I added allauth to a project of mine and installed it with the INSTALLED_APPS and to the urlpatterns how you did, and I get the following URL patterns:

/google/login/	allauth.socialaccount.providers.oauth2.views.view	google_login
/google/login/callback/	allauth.socialaccount.providers.oauth2.views.view	google_callback

It’s likely that 'allauth.socialaccount.providers.google', isn’t being registered as an INSTALLED_APPS element. Check that you’re using the correct settings file and/or what the values are. Or if anything manipulates it.

You could also try verifying what version of allauth you have installed and compare to what the latest version of allauth.

Hmm, let me try reinstalling it again. Btw, this show_urls method is so cool, thanks!

1 Like

allauth.socialaccount.providers.google', is in myINSTALLED_APPS list. So I am not sure what went wrong. But I think you might be on the right track.
After I reinstalled it and I got a different error. It’s coming from the files from allauth:

Hmm, the error above occurred because I changed the path to path('accounts/', include('allauth.urls')),, and now that I changed the path back to path('', include('allauth.urls')),, it’s this error again. Quite interesting.

Found anything on this? I’m also facing the same.

Nope. Taking a break on this. But let me know if you found a solution…

Today I have faced a similar problem.

I tried everything, but nothing worked, so I deleted OAuth 2.0 Client and created a new one.
Added client_id and client_screat

SOCIALACCOUNT_PROVIDERS = {
‘google’: {
‘SCOPE’: [‘email’],
‘AUTH_PARAMS’: {‘access_type’: ‘online’},
‘OAUTH_PKCE_ENABLED’: True,

    'APP': {
        'client_id': env('CLIENT_ID'),
        'secret': env('SECRET'),
    }
}

}

Now everything works well.

go and create new credentials and add them to your SOCIALACCOUNT_PROVIDERS

have a good time.

1 Like

Yea well this worked for me!

you can try this
in settings.py set
SITE_ID = 2