Python-如何使用Django获取Facebook用户朋友列表,状态更新等?

时间:2015-08-03 15:02:16

标签: python django facebook

我在找到如何在接受所有权限后从Facebook用户获取数据时遇到了一些麻烦。现在我只是想让我的Django应用程序登录(完成),并将他们的朋友列表打印到网页,以确保它正常工作。我已经尝试过在这些网页上发布的方法 -

http://axiacore.com/blog/how-retrieve-facebook-extra-info-from-django/

How to retrieve Facebook friend's information with Python-Social-auth and Django

这是我到目前为止的代码 -

from django.shortcuts import render_to_response, render
from django.template.context import RequestContext
import urllib2
from urllib2 import urlopen
import json

def friendlist(request):
    social_user = request.user.social_auth.filter(
                                                    provider='facebook',
                                                ).first()    
    goodies = []
    if social_user:
        #print 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:
        goodies.append(friend.get('name'))
    return render_to_response('thirdauth/friendlist.html', {'friends': goodies})


def home(request):
    context = RequestContext(request,
                       {'request': request,
                        'user': request.user})
    #print request.user
    return render_to_response('thirdauth/home.html',
                         context_instance=context)

然后,当我打印出好东西时,它根本就没有数据。我只是想知道我做错了什么。

我的settings.py文件可能不需要被篡改,但这就是我用它做的。

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'social.apps.django_app.default',
    'thirdauth',
)

TEMPLATE_CONTEXT_PROCESSORS = (
   'django.contrib.auth.context_processors.auth',
   'django.core.context_processors.debug',
   'django.core.context_processors.i18n',
   'django.core.context_processors.media',
   'django.core.context_processors.static',
   'django.core.context_processors.tz',
   'django.contrib.messages.context_processors.messages',
   'social.apps.django_app.context_processors.backends',
   'social.apps.django_app.context_processors.login_redirect',
)

AUTHENTICATION_BACKENDS = (
   'social.backends.facebook.FacebookOAuth2',
   'social.backends.google.GoogleOAuth2',
   'social.backends.twitter.TwitterOAuth',
   'django.contrib.auth.backends.ModelBackend',
)

SOCIAL_AUTH_FACEBOOK_KEY = '1*************'
SOCIAL_AUTH_FACEBOOK_SECRET = '43************************'

FACEBOOK_EXTENDED_PERMISSIONS = ['email']

SOCIAL_AUTH_FACEBOOK_SCOPE = [
    'publish_actions',
    'email',
    'user_friends',
    'user_birthday',
    'public_profile',
    'user_about_me',
    'user_likes',
    'user_location',
    'user_photos',
    'user_posts',
    'user_relationships',
    'user_status',
    'user_tagged_places',
    'user_work_history',
]

urls.py看起来像这样 -

from django.conf.urls import patterns, include, url
from django.contrib import admin

from thirdauth import views

admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'thirdauth.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'thirdauth.views.home', name='home'),
    url('', include('social.apps.django_app.urls', namespace='social')),
    url('', include('django.contrib.auth.urls', namespace='auth')),
    url('^friends/', views.friendlist, name="friendlist"),
)

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

Facebook更改了他们的API(现在只有v2,不再支持v1),因此您无法通过API获取朋友列表。您唯一能做的就是获取使用您应用的用户列表。要测试这是有效的,请确保BOTH用户已添加该应用。

如果您仍然需要获取用户列表,可以按照此链接中的说明导出到Yahoo来执行此操作:http://iag.me/socialmedia/how-to-transfer-your-facebook-friends-to-twitter-and-other-networks-an-update/

我无法正常工作但发现了这个:

http://www.techgainer.com/import-facebook-contacts-gmail-google-contacts/