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

查看:121
本文介绍了如何使用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']

然后,使用这个令牌,您可以发送所需的请求,例如,该代码通过身份,名称,位置和图片获取已验证用户的所有朋友: p>

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 中设置您的权限:

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

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

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