0

I'm trying to get (and save) the user city or location using django and python-social-auth. It will be loaded on the first time the user logging, so I created a PIPELINE

  """
SOCIAL AUTH
"""
SOCIAL_AUTH_PIPELINE = (
    ...
    'libs.util.associate_by_email',
    'libs.util.load_data_new_user',
     ...
)
SOCIAL_AUTH_FACEBOOK_KEY = "...."
SOCIAL_AUTH_FACEBOOK_SECRET = "...."
SOCIAL_AUTH_FACEBOOK_SCOPE = [
    'email',
]

And the pipeline are looking like this:

def load_data_new_user(backend, response, user, *args, **kwargs):
print kwargs['details']
if user is None:
    if backend.name == "facebook":
        try:
            url = "http://graph.facebook.com/%s/picture?width=200&height=200&redirect=false" % response['id']
            data = json.loads(urllib2.urlopen(url).read())['data']
            print data
            return {'avatar': data}
        except StandardError:
            return {'avatar': None}
    else:
        raise ValueError()

It's working to get the avatar, but how can I catch the user city?

Max Ferreira
  • 468
  • 1
  • 4
  • 19

1 Answers1

2
  1. Add the user_location scope to the SOCIAL_AUTH_FACEBOOK_SCOPE setting
  2. Get the location by doing response['location'] in a pipeline (users that didn't fill their location in their profile won't have that key, so check for it first)
omab
  • 3,721
  • 19
  • 23
  • user_location was exatcly what I'm looking for. Thank you very much, its a honour receive a message coming from you. Congrats for this great job. – Max Ferreira Jan 22 '15 at 13:42
  • How can I do the same for "events"? I want to get the user_events, so I put it using SOCIAL_AUTH_FACEBOOK_SCOPE = [ 'email', 'user_location', 'user_events' ] and it worked for Facebook, but doesn't worked to receive events on response[]. – Max Ferreira Jan 22 '15 at 14:17
  • You need to call the corresponding API for that, the scope only grants you access to that particular endpoint, check the docs at https://developers.facebook.com/docs/graph-api/reference/v2.2/user/events. – omab Jan 23 '15 at 03:45