如何从python-social-auth获取用户的生日

时间:2018-04-04 13:28:30

标签: django python-social-auth

所以我已经设置了我的Django应用程序,我希望从Facebook获得生日并将其保存在管道中的某个地方。我确实要求范围:

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email','user_birthday']

类似问题的大多数答案都使用Facebook返回的response字典。但是,当我打印它时,我有这个:

{
'id': .., 
'expires': .., 
'name': .., 
'granted_scopes': ['user_birthday', 'email', 'public_profile'], 
'access_token': ..
}

我如何获得实际生日?

2 个答案:

答案 0 :(得分:2)

现在您拥有了访问令牌,您可以使用它来发送获取请求:

url = 'https://graph.facebook.com/%d?fields=birthday' % user_id
parameters = {'access_token': TOKEN}
r = requests.get(url, params = parameters)
result = json.loads(r.text)

答案 1 :(得分:1)

显然,您使用access_token与FB Graph API进行通信。其中一种方法是手动发送请求,如this other answer所述。然而,我最终做到这一点的方式是使用facepy库,这在眼睛上更容易:

from facepy import GraphAPI

def work_with_response(response, *args, **kwargs):
    graph = GraphAPI(response['access_token'])
    fb_data = graph.get('me?fields=birthday')
    date = fb_data['birthday']