Ravaal
Ravaal

Reputation: 3359

Python- How do I get a Facebook users friend list, status updates, etc. using Django?

I'm having a bit of trouble finding out how to get data back from the facebook user after they accept all permissions. Right now I'm just trying to make my Django app log them in (accomplished), and print their friend list to a webpage just to make sure that it's working properly. I've tried the methods posted on these webpages-

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

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

Here is the code that I have so far-

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)

Then, when I make it print out goodies it has no data at all. I'm just wondering what I'm doing wrong.

My settings.py file probably doesn't need to be tampered with, but this is what I've done with it.

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 looks like this-

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"),
)

Thank you so much for the help!

Upvotes: 1

Views: 2647

Answers (1)

xxx
xxx

Reputation: 1485

Facebook have changed their API (it is now v2 only, v1 is not supported any more) so that you can no longer get a list of friends through the API. The only thing you can do is get a list of users that use your app. To test this is working ensure that BOTH users have added the app.

If you still need to get a list of users you can do it via exporting to Yahoo by following the instructions at this link: http://iag.me/socialmedia/how-to-transfer-your-facebook-friends-to-twitter-and-other-networks-an-update/

I had trouble getting that working but found this:

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

Upvotes: 1

Related Questions