简体   繁体   中英

Get user email in Python-social-auth

I have included this in my settings.py file:

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

how do i access facebook user's email in pipeline. I am using pythons-social-auth

I want to use user email address from facebook and check if a user with that email is already present or not.

You will have to customize a pipeline method or add a new one to the pipeline.

For example, you could customize a create_user method:

def create_user(strategy, details, user=None, *args, **kwargs):
    email = details.get('email', '')

    if UserClass.objects.filter(email=email).exists():
        // do whatever you want

    if user:
        return {'is_new': False}

    fields = dict((name, kwargs.get(name) or details.get(name))
                  for name in strategy.setting('USER_FIELDS',
                                               USER_FIELDS))
    if not fields:
        return

    return {
        'is_new': True,
        'user': strategy.create_user(**fields)
    } 

then in your settings file, indicate a path to this method:

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',
    'path.to.your.custom.create_user',    // indicate the path here
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details'
)

hope this helps

UPDATE:

If you are not getting email in the details parameter, try this:

Print the response variable in social_details method of pipeline:

def social_details(backend, details, response, *args, **kwargs):
    print response
    return {'details': dict(backend.get_user_details(response),
                            **details)}

see in the logs if response contains email . If it does contain email , it means that email is just not being included in the details parameter in details variable.

If not, is SOCIAL_AUTH_FACEBOOK_SCOPE = ['email'] working? I mean, is Facebook asking you to grant this scope when you login?

If yes, grant it. If no, try to delete your app from facebook, and register again. It sometimes happens.

This may be an old question but the answer is import to lots of people. So here we go.

First you grant the scope , then you harvest the data. So your django settings.py file needs these two settings (in addition to key and secret )

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
  'locale': 'en',
  'fields': 'id, name, email,
}

Be aware that during authentication the user may decide to not supply the email even though you've asked for it. So instead of getting email with response['email'] use response.get('email', None) . Then handle the case where email is not provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM