Python-social-auth获取用户城市

时间:2015-01-21 21:16:23

标签: django python-social-auth

我正在尝试使用django和python-social-auth获取(并保存)用户城市或位置。它将在用户首次登录时加载,因此我创建了一个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',
]

管道看起来像这样:

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()

它正在努力获取头像,但我怎样才能抓住用户城市?

1 个答案:

答案 0 :(得分:2)

  1. user_location范围添加到SOCIAL_AUTH_FACEBOOK_SCOPE设置
  2. 通过在管道中执行response['location']来获取位置(未在其个人资料中填充其位置的用户将没有该密钥,因此请先检查它)