Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On facebook login: AttributeError at /complete/facebook/ 'NoneType' object has no attribute 'expiration_datetime' #190

Closed
mohitranka opened this issue Feb 15, 2014 · 27 comments

Comments

@mohitranka
Copy link

Environment:


Request Method: GET
Request URL: http://localhost:8000/complete/facebook/?redirect_state=sGJcpBJYrR7EPzX3ATNMBeLPZP9rKse8&code=AQCFaLPqPLOyw0_tL23fb0-sg_0E4d4Dtng_XnPvpDG5jg8vHH2I31M_XGsUOFBcu6KaJjeXsY-yWIH26H-_X1_1X5fYauzhJ8dS0uo2p1uS7nS3HUykIJADbm6a7Gmcey_8Ehh94RJXFdixpZfu-irUSryamD4f9xMbkOOLvdzzwWd35qZGtTLlwWYGJktxTDaynl6qAlf_aE8-8t6f5zI7y4TdoiCk7VP2HdM6CtXJJ8Qu53U-HYlUuKL-F_v_WzvO6Fq-5bYbMCgofCGM0-dNZd8getQsGTQtQTW3rD1nNDvn0oQQnbmw1P95Ssz0yjQ&state=sGJcpBJYrR7EPzX3ATNMBeLPZP9rKse8

Django Version: 1.6
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'app1',
 'south',
 'django_extensions',
 'social.apps.django_app.default')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/mohit/.virtualenvs/app1/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mohit/.virtualenvs/app1/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/home/mohit/.virtualenvs/app1/local/lib/python2.7/site-packages/social/apps/django_app/utils.py" in wrapper
  45.             return func(request, backend, *args, **kwargs)
File "/home/mohit/.virtualenvs/app1/local/lib/python2.7/site-packages/social/apps/django_app/views.py" in complete
  21.                        redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "/home/mohit/.virtualenvs/app1/local/lib/python2.7/site-packages/social/actions.py" in do_complete
  61.             login(strategy, user)
File "/home/mohit/.virtualenvs/app1/local/lib/python2.7/site-packages/social/apps/django_app/views.py" in _do_login
  44.         expiration = social_user.expiration_datetime()

Exception Type: AttributeError at /complete/facebook/
Exception Value: 'NoneType' object has no attribute 'expiration_datetime'
@mohitranka
Copy link
Author

Its similar to #172 (but with facebook as the backend). I am using the latest available release (0.1.21).

Django==1.6
MySQL-python==1.2.4
South==0.8.4
argparse==1.2.1
distribute==0.6.34
django-extensions==1.2.5
ipython==1.2.0
oauthlib==0.6.1
python-dateutil==2.2
python-openid==2.2.5
python-social-auth==0.1.21
requests==2.0.1
requests-oauthlib==0.4.0
six==1.5.2
twython==3.1.2
wsgiref==0.1.2

@omab
Copy link
Owner

omab commented Feb 16, 2014

Do you have a custom pipeline?

@mohitranka
Copy link
Author

My pipeline is

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    'myapp.pipeline.finalize',
)

Which is standard + custom last step. I'll look into running the step with commenting out the last step of the pipeline and check if the error still exists.

@omab
Copy link
Owner

omab commented Mar 1, 2014

What does the last pipeline?

@devangmundhra
Copy link

I am getting the same issue too with

python-social-auth==0.1.22
Django Version: 1.6.2
Python Version: 2.7.5

My pipeline is

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.social_auth.associate_by_email',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details'
)

@omab
Copy link
Owner

omab commented Mar 15, 2014

I've introduced a change that might fix the issue, @mohitranka and @devangmundhra could you give it a try?

@devangmundhra
Copy link

Hi Matias,

Somehow I don't see the error any more even before trying the fix (I
uninstalled and reinstalled the same version of python-social-auth).
If I start seeing this error again, I'll try the fix you have mentioned.

Thanks.

On Sat, Mar 15, 2014 at 10:39 AM, Matías Aguirre
notifications@github.comwrote:

I've introduced a change that might fix the issue, @mohitrankahttps://github.com/mohitrankaand
@devangmundhra https://github.com/devangmundhra could you give it a try?

Reply to this email directly or view it on GitHubhttps://github.com//issues/190#issuecomment-37732368
.

@CrazyPilot
Copy link

Have the same problem on facebook, twitter and VK login((

@omab
Copy link
Owner

omab commented Mar 20, 2014

@CrazyPilot, have you tried my fix at dec6d30 ?

@ghost
Copy link

ghost commented Mar 23, 2014

@omab, it doesn't help me. I also have such error with facebook and twitter.

@omab
Copy link
Owner

omab commented Mar 23, 2014

@sledopit, I've pushed another potential fix, could you give it a try?

@ghost
Copy link

ghost commented Mar 23, 2014

@omab. nope it also didn't fix it. some technical details:
$ pip freeze Django==1.6.2 South==0.8.4 anyjson==0.3.3 argparse==1.2.1 beautifulsoup4==4.3.2 distribute==0.6.24 django-htmlmin==0.6.3 django-registration==1.0 django-tinymce==1.5.2 oauthlib==0.6.1 python-openid==2.2.5 -e git+https://github.com/omab/python-social-auth.git@062552926c30379d13058ad11a3c9a76663c9b95#egg=python_social_auth-dev pytz==2013.9 requests==2.2.1 requests-oauthlib==0.4.0 six==1.6.1 sorl-thumbnail==11.12 sqlparse==0.1.11 timezones==1.6 wsgiref==0.1.2
Trace:

Environment:


Request Method: GET
Request URL: http://localhost/complete/facebook/?redirect_state=U53FUiZTj6TAzFH4RYrpffKEDfK3ORfW&code=AQAYwwCXDgN05xlpAOjvq6esW46JmFUdCTsHpQdQ41hi4yPc7iQ9OwUxQr0k3-yaVoq9GTtvIjYuGrZx6OfEBUn7joQZWmT1AGVOkMg4GnyD1HmryhoBeopII6HiBF2Q-Bu6bDp5xZUdMkwk_N7Mdr4wGAeF9QwjMg1D6jpHpxksaSWdGGlJa6JNDpBCY5mwVqFRVB51ez7ToQZP9N6UyyeX0Hf4OsMRVVyHajqrntFyw-2LTmSVJpI5cIT4d9gmAopzbqyk-3RCjnpxCHNYFrDtoL_UKf7TN8qcmBrY80ZACKWbcpN93rZrB0Dd4ijtKl4&state=U53FUiZTj6TAzFH4RYrpffKEDfK3ORfW

Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('south',
 'gdata',
 'captcha',
 'timezones',
 'registration',
 'sorl.thumbnail',
 'django.contrib.auth',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.contenttypes',
 'social.apps.django_app.default',
 'site.main',
 'grappelli',
 'django_extensions',
 'django.contrib.admin')
Installed Middleware:
('htmlmin.middleware.HtmlMinifyMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/site/www/.env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/site/www/.env/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/home/site/www/.env/lib/python2.7/site-packages/social/apps/django_app/utils.py" in wrapper
  45.             return func(request, backend, *args, **kwargs)
File "/home/site/www/.env/lib/python2.7/site-packages/social/apps/django_app/views.py" in complete
  21.                        redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "/home/site/www/.env/lib/python2.7/site-packages/social/actions.py" in do_complete
  61.             login(strategy, user, social_user)
File "/home/site/www/.env/lib/python2.7/site-packages/social/apps/django_app/views.py" in _do_login
  41.         expiration = social_user.expiration_datetime()

Exception Type: AttributeError at /complete/facebook/
Exception Value: 'NoneType' object has no attribute 'expiration_datetime'

it is almost the same as in OP trace.

@omab
Copy link
Owner

omab commented Mar 23, 2014

@sledopit, I'm not able to reproduce this issue, that's why it's so hard for me to fix it, could you trace back why social_user is None?

@ghost
Copy link

ghost commented Mar 23, 2014

@omab it's quite a challenge for me. I will try it, but I definitely need some time for investigations.
By the way, can this be related due to I'm using a custom User Model?
( SOCIAL_AUTH_USER_MODEL and AUTH_USER_MODEL are defined in settings.py )

@devangmundhra
Copy link

@omab: FYI, I was also using a custom User Model when I was getting this
error.
@sledopit: I am sure you know this, but still just FYI, it could help in
debugging if you used python's debugger because I am not much of a python
developer and this was a really helpful tool when I got to know about it.
Just 'import pdb' in social/apps/django_app/utils.py and set
'pdb.set_trace()' before line 45. You can then print (p) and step (s)
through the functions to see why social_auth is None. More info about pdb
here http://docs.python.org/2/library/pdb.html.

On Sun, Mar 23, 2014 at 11:32 AM, sledopit notifications@github.com wrote:

@omab https://github.com/omab it's quite a challenge for me. I will try
it, but I definitely need some time for investigations.
By the way, can this be related due to I'm using a custom User Model?
( SOCIAL_AUTH_USER_MODEL and AUTH_USER_MODEL are defined in settings.py )

Reply to this email directly or view it on GitHubhttps://github.com//issues/190#issuecomment-38391840
.

@omab
Copy link
Owner

omab commented Mar 23, 2014

@sledopit, @devangmundhra, I've tried a custom user model (it's in the example app) and it worked without issues, if any of you can build a simple app that reproduces the issue that I can clone and try here, please do.

@ghost
Copy link

ghost commented Mar 24, 2014

@omab, I tried to make a simple app to reproduce and there it works without this error. This is really strange as all things are absolutely the same (even User model app is just copied from my project ). I will try to find the diff between simple app and my. Hope I'll be lucky enough to catch the root.

@htch
Copy link

htch commented Apr 1, 2014

I encounter the same issue (vkontakte and odnoklassniki backends for me), as far as i can remember it has also appeared when i introduced a custom User model. I use the default pipeline.

@omab
Copy link
Owner

omab commented Apr 1, 2014

@htch, version?

@htch
Copy link

htch commented Apr 1, 2014

@omab, 0.1.23

$ pip freeze
Django==1.6.2
MySQL-python==1.2.5
Pillow==2.3.1
South==0.8.4
argparse==1.2.1
boto==2.27.0
distribute==0.6.24
django-bower==4.8.1
django-debug-toolbar==1.0.1
django.js==0.8.1
djangorestframework==2.3.13
ipython==1.2.1
oauthlib==0.6.1
python-openid==2.2.5
python-social-auth==0.1.23
requests==2.2.1
requests-oauthlib==0.4.0
six==1.6.1
sqlparse==0.1.11
wsgiref==0.1.2

@devangmundhra
Copy link

I have seen a similar problem occurring with another open source project
for Django- tastypie.
There too, the problem occurred intermittently and was difficult to
reproduce once it was gone.

I was able to find the cause of the problem in that instance, which was
that while loading the tastypie modules, it tried to import
CustomUserModule. However, if the custom user module model has not been
initialized by then, Django throws an ImportError and the 'User' is set as
None.
Now the next time the 'User' field is accessed, it is of NoneType leading
to this problem.

One way to solve the problem there was to reload the
modules(tastypie.compat) from which the User module was accessed.
More description of the problem found in tastypie is available here-
https://github.com/toastdriven/django-tastypie/issues/1009

I am not sure if this problem is related or not, though the symptoms are
very similar.
So if the social_auth_user_models is loaded before the actual user model is
loaded, this issue can occur.

On Tue, Apr 1, 2014 at 10:43 AM, Pavel Kuznetsov
notifications@github.comwrote:

I encounter the same issue (vkontakte and odnoklassniki backends for me),
as far as i can remember it has also appeared when i introduced a custom
User model. I use the default pipeline.

Reply to this email directly or view it on GitHubhttps://github.com//issues/190#issuecomment-39235430
.

@omab
Copy link
Owner

omab commented Apr 1, 2014

@htch, that version has my possible fixes to the issue, so they aren't working, are you able to create a simple small project that recreates the issue?

@ghost
Copy link

ghost commented Apr 1, 2014

@htch , check you database. when I recreated it from scratch everything became fine. I didn't figure out what was wrong in database, but the evil is in it. I still have both databases and with the old one I still can reproduce the bug. So it looks like some migration broke it at my side.

@htch
Copy link

htch commented Apr 1, 2014

I guess it does indeed have a lot to do with the state of the database.
In my case the culprit was the exception suppressed here

except Exception as err:

(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mi8`.`social_auth_usersocialauth`, CONSTRAINT `user_id_refs_id_e6cbdf29` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')

Once I deleted the foreign key (obviously left from the time when I have switched from auth.User to my custom user model), the error disappeared.

@dulacp
Copy link

dulacp commented Apr 16, 2014

@htch +1 Same thing for me, the following database integrity error

ERROR:  insert or update on table "social_auth_usersocialauth" violates foreign key constraint "social_auth_usersocialauth_user_id_fkey"
DETAIL:  Key (user_id)=(66) is not present in table "auth_user".

fails silently because is catch by the line

except Exception as err:
(as @htch found out)

@bemall
Copy link

bemall commented Feb 4, 2015

Any resolution to this ?
Dajngo 1.7 and Python 2.7

@ezesundayeze
Copy link

This problem is generated from the database. It happened to me when I deleted a user through PHPMyAdmin without deleting its relationships. When I deleted dropped the database and made a new migration, it was all gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants