[Solved]-Debugging django-allauth Social Network Login Failure

28👍

More information is passed to the context used to render the error template but is not used in the default template.

You can get log output by overriding the template and including in your template the following:

{{ auth_error }}

or alternatively:

Code: {{ auth_error.code }}, Error: {{ auth_error.exception }}

To override the template, add a folder to your Django template DIRS. In Django 1.8+, this looks like the following:

TEMPLATES = [
    {
        ...
        DIRS: [os.path.join(BASE_DIR, 'templates')]
    }
]

Then, in that folder, make directory socialaccount and put in it a file called authentication_error.html

👤Zags

15👍

If you use a custom SocialAccountAdapter (can be set in your settings.py, read more here) then you can simply overwrite the function authentication_error to log all of the errors.

The function signature (source here) looks like this:

(main/wherever.py):

class SocialAccountAdapter(DefaultSocialAccountAdapter):
    def authentication_error(self, request, provider_id, error, exception, extra_context):
        your_log_function(
            'SocialAccount authentication error!',
            'error',
            request,
            extra_data = {'provider_id': provider_id, 'error': error.__str__(), 'exception': exception.__str__(), 'extra_context': extra_context},
        )

(and in settings.py)

SOCIALACCOUNT_ADAPTER = "main.wherever.SocialAccountAdapter"

I’m doing this in my app and it works great!

4👍

@Zags answers is great and pointed me in the right direction.

Building on that answer, I suggest start by copying the default authentication_error.html to your tree, e.g.:

$ find /wherever/your/python/is -name socialaccount
$ cp .../that/dir/socialaccount/authentication_error.html \
     ./myapp/templates/allauth/socialaccount/

Then add in to the copy of the template:

<p>
Code: {{ auth_error.code }}, Error: {{ auth_error.exception }}
</p>

(Technically you could just edit the installed python version…)

Result for me, slightly formatted:

Code: unknown, 
Error: Error retrieving access token: 
b'{"error":{
  "message":"This IP can\'t make requests for that application.",
  "type":"OAuthException",
  "code":5,
  "fbtrace_id":"..."
}}'

2👍

“Social Network Login Failure” can occur due to multiple reasons but URL mismatch is the most frequent problem I have experienced:

  1. If you are facing this problem while hosting on third party, do remember to change expected URL to host’s url

  2. If the problem is occuring during development only, you must have registered on social website using localhost or 127.0.0.1 . Do call social website through correct url. For e.g. facebook doesn’t allow to register 127.0.0.1 but localhost and “manage.py runserver” gives 127.0.0.1 as default. So open your website using “localhost”.

Also ensure that you have added correct url to ‘sites’ in django admin and it has been correctly added to ‘social application’

👤Sid

2👍

This also happened to me when I was checking https site on http (if I could redirect production). Error code/message was empty.
I commented out these for the http testing:

# SESSION_COOKIE_SECURE = True
# CSRF_COOKIE_SECURE = True
# APPEND_SLASH = True
# PREPEND_WWW = True

0👍

When I was testing locally, my problem was two fold:
First, I had to make sure that I started the server using

python manage.py runserver localhost:8000

Then, in the admin page I had to make sure the Sites table had an entry for localhost:8000, e.g. at

http://localhost:8000/en/admin/sites/site/

The gotcha for me was that I’d had multiple attempts at this and the result was the the ID for my localhost:8000 Sites table entry had become greater than 1. You’ll notice in the allauth documentation that is says to to have SITE_ID = 1 in the settings.py but if you inadvertently create a different ID for the entry, you’ll have to either alter the database entry’s ID or change SITE_ID value.

The other issue I had (unrelated to the poster’s issue) was that I also had old registrations url template include interfering with the allauth ones.

0👍

In my case I had everything ok except the flag SESSION_COOKIE_DOMAIN = mydomain.com which I only had to comment because I was working on localhost and not in mydomain.com.

Leave a comment