axiacore

Blog

How to retrieve Facebook extra info from Django

Vera Mazhuga

Vera Mazhuga

Published

1 min read.


When we use python-social-auth sometimes there is a need to obtain
some extra information from the Facebook about a registered user. Let's look at
how to retrieve a list of friends and their location.


To specify to which fields we need to have access, we need to add them to SOCIAL_AUTH_FACEBOOK_SCOPE list in settings.py file:

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


So when a user will try to log in to our website using his Facebook account,
he
will be asked
if our application can access his personal data:



In our case the app will ask him for his email, list of his friends and their location.


If the user agrees,
python-social-auth will create an instance of SocialUser clas with the following attributes:



As we can see, accessing
extra_data JSON, we can get a Facebook
token
will allow us to get the extra information we need via Facebook API.
Firstly, we get an
SocialUser object:


<pre lang="python">social_user = request.user.social_auth.filter(<br/> provider='facebook',<br/>).first()<br/></pre>


Since we got it, we can sent a GET request to Facebook API, passing
the user's
uid, access token and a list of fields we want to obtain:

if social_user:
    url = u'<a href="https://graph.facebook.com/{0}/'">https://graph.facebook.com/{0}/'</a> \
          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)


The API returns us a JSON with the data we requested:

{
   "data":[
      {
         "id":"uid",
         "name":"John Doe",
         "location":{
            "id":"id",
            "name":"Bogotá, Colombia"
         },
         "picture":{
            "data":{
               "url":"avatar url",
               "is_silhouette":false
            }
         }
      }
   ]
}

So now we can load it co a Python dictionary:

    friends = json.loads(urllib2.urlopen(request).read()).get('data')
    for friend in friends:
        location = friend.get('location')
        # do something
Get updates from our CEO every other week about axiacore:

Learn about how we think:

Imagen vs. Diseño digital

Durante muchos años, se ha venido hablando sobre que la buena imagen de una página web, trae consigo más clientes y una mayor...

Roberto Aguirre

Roberto Aguirre

Published Apr 04, 2014

Talk Review: Building a RESTfull API Using Django

Some days ago I gave a talk on how to create REST APIs usingDjango Rest Framework, the idea was to share some of the thi...

Pablo Vallejo

Pablo Vallejo

Published Apr 14, 2014

How to use GenericForeignKey in Django

Sometimes, when the solution of the problem is associated with certain... uhm... difficulties, especially when you are still ...

Vera Mazhuga

Vera Mazhuga

Published Apr 10, 2014

We build software solutions from idea to market.

Let's Talk