如何使用 Python-Social-auth 和 Django 检索 Facebook 好友的信息 [英] How to retrieve Facebook friend's information with Python-Social-auth and Django

查看:26
本文介绍了如何使用 Python-Social-auth 和 Django 检索 Facebook 好友的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Python-Social-auth 和 Django 检索 Facebook 好友的信息?我已经检索了个人资料信息并对用户进行了身份验证,但我想获取有关他们朋友的更多信息并邀请他们使用我的应用程序.谢谢!

How can I retrieve Facebook friend's information using Python-Social-auth and Django? I already retrieve a profile information and authenticate the user, but I want to get more information about their friends and invite them to my app. Thanks!

推荐答案

您可以使用 Facebook API 来完成.首先,您需要获取 Facebook 应用程序的令牌 (FACEBOOK_APP_ACCESS_TOKEN) https://developers.facebook.com/tools/accesstoken/或来自 social_user.extra_data['access_token']

You can do it using Facebook API. Firstly, you need obtain the token of your Facebook application (FACEBOOK_APP_ACCESS_TOKEN) https://developers.facebook.com/tools/accesstoken/ or from social_user.extra_data['access_token']

然后用这个令牌你可以发送你需要的请求,例如,这个代码获取所有经过身份验证的用户的朋友,包括他们的 id、姓名、位置、图片:

Then with this token you can send the requests you need, for example, this code gets all the friends of the authenticated user with their id, name, location, picture:

social_user = request.user.social_auth.filter(
    provider='facebook',
).first()
if social_user:
    url = u'https://graph.facebook.com/{0}/' 
          u'friends?fields=id,name,location,picture' 
          u'&access_token={1}'.format(
              social_user.uid,
              social_user.extra_data['access_token'],
          )
    request = urllib2.Request(url)
    friends = json.loads(urllib2.urlopen(request).read()).get('data')
    for friend in friends:
        # do something

根据您想要获取的字段,您可以在此处设置权限:https://developers.facebook.com/apps/ -> 你的应用 -> 应用详情 ->应用中心权限

Depending on what fields you want to get you can set the permissions here: https://developers.facebook.com/apps/ -> Your App -> App Details -> App Centre Permissions

或在 settings.py 中设置您的权限:

or set your permissions in settings.py:

SOCIAL_AUTH_FACEBOOK_SCOPE = [
    'email',
    'user_friends',
    'friends_location',
]

这篇关于如何使用 Python-Social-auth 和 Django 检索 Facebook 好友的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆