How to setup the SOCIAL_AUTH_PIPELINE?

478 views
Skip to first unread message

Hooman

unread,
Apr 8, 2013, 5:11:49 AM4/8/13
to django-so...@googlegroups.com
Hi guys,


I tried to reply to an existing thread but the post didn't get through.


I came across this thread and this is pretty much what I am trying to achieve:

https://groups.google.com/forum/?fromgroups=#!topic/django-social-auth/fVldpqs1jHg


SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.associate.associate_by_email',
    'social_auth.backends.pipeline.user.get_username',
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',

    'project_app.utils.facebook_save',
)

I don't know how exactly to point to the module in the last line.

My project structure looks like this: (I am using virtual-env)

/home/project-env/bin  (python is in here)
/home/project-env/site (whole django project is in here)
/home/project-env/site/project (Settings.py, url.py etc are here)
/home/project-env/site/project_app (models.py and utils.py etc are here)


Inside project_app I created a file called util.py and inserted the facebook_save method.  But the path inside
SOCIAL_AUTH_PIPELINE, doesnt work at all. 

What am I missing please?

Many Thanks,
Hooman

Serdar Dalgic

unread,
Apr 8, 2013, 5:46:01 AM4/8/13
to django-so...@googlegroups.com
On Mon, Apr 8, 2013 at 12:11 PM, Hooman <hou...@gmail.com> wrote:
> Hi guys,

Hi;

>
>
> SOCIAL_AUTH_PIPELINE = (
...
> 'project_app.utils.facebook_save',
> )
>

You wrote "utils"

>
> Inside project_app I created a file called util.py and inserted the
> facebook_save method. But the path inside
> SOCIAL_AUTH_PIPELINE, doesnt work at all.
>

But the name of the file is util.py

> What am I missing please?
>

just a letter: "s" :)

Regards.

--
- Serdar Dalgıç <s...@serdardalgic.org>
FLOSS Developer, Life & Nature Hacker
twitter: https://twitter.com/serdaroncode

houmie

unread,
Apr 8, 2013, 9:07:41 AM4/8/13
to django-so...@googlegroups.com
Hi,

Thank you for your reply. Sorry I have made a mistake in the email.
I can confirm that both the file is utils.py and the path in SOCIAL_AUTH_PIPELINE is also utils.

Am I not missing anything else? :(

Thanks




On 08/04/13 10:46, Serdar Dalgic wrote:
On Mon, Apr 8, 2013 at 12:11 PM, Hooman <hou...@gmail.com> wrote:
Hi guys,
Hi;


SOCIAL_AUTH_PIPELINE = (
...
    'project_app.utils.facebook_save',
)

You wrote "utils"

Inside project_app I created a file called util.py and inserted the
facebook_save method.  But the path inside
SOCIAL_AUTH_PIPELINE, doesnt work at all.

But the name of the file is util.py

What am I missing please?

just a letter: "s" :)

Regards.



Hooman Korasani, CTO
www.watchfit.com 

WatchFit | 35 Kingsland Road | London E2 8AA

Mobile: 44 (0) 7787 454077

Facebook Twitter LinkedIn

Serdar Dalgic

unread,
Apr 8, 2013, 9:12:30 AM4/8/13
to django-so...@googlegroups.com
On Mon, Apr 8, 2013 at 4:07 PM, houmie <hou...@gmail.com> wrote:
Hi,

Thank you for your reply. Sorry I have made a mistake in the email.
I can confirm that both the file is utils.py and the path in SOCIAL_AUTH_PIPELINE is also utils.

Am I not missing anything else? :(


Can you try whether you can import facebook_save function from django shell? I mean

$ python manage.py shell
> from project_app.utils import facebook_save

If you can not, can you check whether you have an __init__.py file under project_app directory?

If you installed DSA correctly, then that must be working, I haven't found any missing thing in your code. 
 

houmie

unread,
Apr 8, 2013, 10:07:08 AM4/8/13
to django-so...@googlegroups.com
Hi Serdar,

That is actually working. Thats a great way to check the path.

In that case the problem must be something else.

I wonder if the function itself is correct:

def facebook_save(user, social_user, detail, response, *args, **kwargs):
    # Save any data on the custom fields in the user model
    user.date_of_birth = response.get('user_birthday')
    user.save()

I can see after clicking on "connect with facebook", it kind of signs the user in with facebook. But the date of birth is still not set on the user model. Maybe it crashes here but social auth doesnt throw any errors for me. :(

Any idea?
twitter: https://twitter.com/serdaroncode --
You received this message because you are subscribed to a topic in the Google Groups "Django Social Auth" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-social-auth/aTN7CbfSipc/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-social-a...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


Hooman Korasani, CTO
www.watchfit.com 

WatchFit | 35 Kingsland Road | London E2 8AA

Serdar Dalgic

unread,
Apr 8, 2013, 1:48:14 PM4/8/13
to django-so...@googlegroups.com
On Mon, Apr 8, 2013 at 5:07 PM, houmie <hou...@gmail.com> wrote:

I wonder if the function itself is correct:

def facebook_save(user, social_user, detail, response, *args, **kwargs):
    # Save any data on the custom fields in the user model
    user.date_of_birth = response.get('user_birthday')
    user.save()

I can see after clicking on "connect with facebook", it kind of signs the user in with facebook. But the date of birth is still not set on the user model. Maybe it crashes here but social auth doesnt throw any errors for me. :(


So, If you're using facebook login, the name of the variable in response is "birthday" not "user_birthday". user_birthday is the name of the permission that you ask the users to grant your application :) if you use response.get('birthday') you'll get what you want. 


Houmie

unread,
Apr 8, 2013, 4:33:20 PM4/8/13
to django-so...@googlegroups.com
Serdar,

Thank you for your help.

I have changed it like you suggested:


def facebook_save(user, social_user, detail, response, *args, **kwargs):
    user.date_of_birth = response.get('birthday')
    user.save()

It still looks like it is crashing when trying to login with facebook, as soon as I have the new 'project_app.utils.facebook_save' in the SOCIAL_AUTH_PIPELINE.  I wished there was a way to get the DSA exceptions thrown to me, so I can see what is wrong. :(

The strange part it seems I get a complete, but still it doesn't login.

[08/Apr/2013 21:31:33] "GET /complete/facebook/?redirect_state=xFVjbxBMC5WhMYgI...qp8Z HTTP/1.1" 302 0
[08/Apr/2013 21:31:33] "GET /login/ HTTP/1.1" 200 2182

These are my settings:

FACEBOOK_EXTENDED_PERMISSIONS = ['email', 'user_birthday', 'user_location']
FACEBOOK_EXTRA_DATA = [('user_birthday', 'user_location')]
SOCIAL_AUTH_REDIRECT_IS_HTTPS = False
SOCIAL_AUTH_RAISE_EXCEPTIONS = True


Any idea why I don't get to see the exceptions raised by Django Social Auth?

Many Thanks,
Hooman

--
~Houmie

Serdar Dalgic

unread,
Apr 8, 2013, 6:08:53 PM4/8/13
to django-so...@googlegroups.com
On Mon, Apr 8, 2013 at 11:33 PM, Houmie <hou...@gmail.com> wrote:
> Serdar,
>
> Thank you for your help.
>

No problem.

>
> It still looks like it is crashing when trying to login with facebook, as
> soon as I have the new 'project_app.utils.facebook_save' in the
> SOCIAL_AUTH_PIPELINE. I wished there was a way to get the DSA exceptions
> thrown to me, so I can see what is wrong. :(
>

Can you define "it is crashing"?

> The strange part it seems I get a complete, but still it doesn't login.
>

Can you check whether the user is deactivated or not?

Here is the line that DSA logins the user
https://github.com/omab/django-social-auth/blob/master/social_auth/views.py#L127

If you want, you can put a break and debug the code.

Just write
import ipdb; ipdb.set_trace()

before the login, and begin debugging the code, if you want. ipdb is a
fine tool that saves lives :)

> [08/Apr/2013 21:31:33] "GET
> /complete/facebook/?redirect_state=xFVjbxBMC5WhMYgI...qp8Z HTTP/1.1" 302 0
> [08/Apr/2013 21:31:33] "GET /login/ HTTP/1.1" 200 2182
>
> These are my settings:
>
> FACEBOOK_EXTENDED_PERMISSIONS = ['email', 'user_birthday', 'user_location']

Yeap, fine. No problem with that.

> FACEBOOK_EXTRA_DATA = [('user_birthday', 'user_location')]

This means you send user_birthday and user_location data for
authenticating with facebook. Are you sure you really need this? You
can try removing this setting.

> SOCIAL_AUTH_REDIRECT_IS_HTTPS = False

That one is default, no need to define in settings.py

> SOCIAL_AUTH_RAISE_EXCEPTIONS = True
>

If you set this value, DSA's exception handling (with the use of DSA
middleware) is disabled. See below for details.

>
> Any idea why I don't get to see the exceptions raised by Django Social Auth?
>

You should use social_auth.middleware.SocialAuthExceptionMiddleware if
you want DSA to handle your exceptions. See
http://django-social-auth.readthedocs.org/en/latest/configuration.html#exceptions-middleware
for details.

I hope you find a solution to your problem, digging out the manuals
would help you much. I'd suggest you to dive into the manuals more.

Regards.

Houmie

unread,
Apr 8, 2013, 6:44:45 PM4/8/13
to django-so...@googlegroups.com
Serdar,

Thanks for the great tip. I am using Aptana STudio, so i was able to
debug it at that spot to suggested. It was a bit further up.

def complete_process(request, backend, *args, **kwargs):
"""Authentication complete process"""
# pop redirect value before the session is trashed on login()
redirect_value = request.session.get(REDIRECT_FIELD_NAME, '') or \
request.REQUEST.get(REDIRECT_FIELD_NAME, '')
# Django 1.5 allow us to define custom User Model, so integrity errors
# can be raised.
try:
user = auth_complete(request, backend, *args, **kwargs)


Literally when I use the custom SOCIAL_AUTH_PIPELINE, I get a NONE user
in the line above.
When I comment out the custom SOCIAL_AUTH_PIPELINE, then I get actually
the facebook user in the line above.

Therefore auth_complete() is the problem.

Since there is no real exception I have no idea why the user objects
remains null. It must be something with the new pipeline that doesn't work.

I read that part of documentation and have added
'social_auth.middleware.SocialAuthExceptionMiddleware', and removed

SOCIAL_AUTH_RAISE_EXCEPTIONS = True.

Further I have setup this in settings:

SIGNUP_ERROR_URL = '/signup-error/'
LOGIN_ERROR_URL = '/signup-error/'

And this in url.py:

url(r'^signup-error/$', TemplateView.as_view(template_name="error.html"), name='signup-error'),


and error.html:

There was an error
{{error}}
{{ValueError}}
{{message}}



I think now I understand why I don't get an exception error. When the user is None due a problem I don't know, no line of the code in the rest of complete_process() raises an exception.

msg is also none.

Hence it goes to the last line: return HttpResponseRedirect(url) where url is /signup-error/.
And this could be the reason why I see no exception error in error.html.

The trick with manual debugging is a big help though, but I wished Django Social AUth would just raise the exceptions and notify me of these problems.

I have removed FACEBOOK_EXTRA_DATA as you suggested. I wonder why I get a null user, it must have to do with the custom SOCIAL_AUTH_PIPELINE, because how else is it otherwise working when I comment it out? But I still have no idea why this is happening....

Thanks,

--
~Houmie

Houmie

unread,
Apr 9, 2013, 6:59:22 PM4/9/13
to django-so...@googlegroups.com
Hi,

For sake of completeness, Matias, the author of DSA has found the problem.

The error is on your facebook_save handler, the function specifies a parameter "detail" but it must be "details".


It was one letter "s". I was debugging for hours and couldn't find this. There is no error message and its really hard to figure these things out.

Hope it helps someone else :)

Many Thanks,

Hooman

--
~Houmie

On 08/04/13 23:08, Serdar Dalgic wrote:

Matías Aguirre

unread,
Apr 9, 2013, 7:42:23 PM4/9/13
to django-social-auth
The missing error message is a bummer, I will take a look into that and any
exception catching that might be too invasive.

Excerpts from Houmie's message of 2013-04-09 19:59:22 -0300:
--
Matías Aguirre (matias...@gmail.com)
Reply all
Reply to author
Forward
0 new messages