This is how I downloaded profile pics of my Facebook friends using Graph API

Hey there !

A few days ago, my friend Hasil wrote a python script to post “Thank you” comments on B’day posts automaticlly using Facebook Graph API. It suddenly arose my interest in Graph API. And, after having developed applications(small/mid level) based on more than 8 APIs till the moment, it was just another API that I was going to work with; But the most complicated one I have ever worked with. So let me share my experience with you all, hope you guys will enjoy it, I surely did enjoy learning and using it.

What is Graph API?

Facebook Graph API

Graph API is HTTP based API(Application Processing Interface), that allows developers to send GET and POST requests to Facebook servers for data exchanging.

HTTP is a standard web protocol that is used all over the world and most of web applications for fetching and sending data to a remote server, of which GET and POST are two defined ways to work with data.

Graph API is HTTP based and hence helps us in fetching,GET request and posting, POST request from Facebook servers really easily without going into complex process underneath and any programming language which has support for these two requests can be used. But Python is my choice of language for some obvious reasons 😉

For identifying these GET/POST requests uniquely for each developer, Facebook uses an access token and all the requests are uniquely identified by Facebook server using this token only. We will shortly get to know how to get the unique access token. So let’s get started.

Getting Acess Token:

As mentioned earlier, everytime we send request(GET/POST) to Facebook, we need to use access token for Facebook servers to identify us as a unique developer. Generating an access token is really easy. Just follow the following simple steps:

  • Open Graph API explorer and click on Graph API Explorer. Now, click on Get Access Token and tick all the options that come under your set of requirement from Graph API. (PS: you can tick all the options including the ones in Extended Permissions)
  • A really long random code will be generated and that’s our access token which is required to work with this amazing Graph API.

Fetching Details of Friends with Script:

For making HTTP requests to Facebook servers, I am going to use Python module Requests. It makes our life really easy(one of the reasons that Python is the language of my choice).

import requests
import json
token = ‘ ‘ # paste the access token that you got in last step, here.
api_url = “https://graph.facebook.com/v2.1/
params = {‘access_token’ : token}
call = “me/friends?fields=picture.width(9999).height(9999).type(large),gender,name”
response = requests.get(api_url + call, params=params)
r = (json.loads(response.content))

And the output will be some JSON encoded object returning the information about your friends. Now you just need to read the JSON object and use information about each of your friends one by one.

Downloading Profile Pictures from the information extracted:

Now, that we have extracted all the information about friends, next job will be to perform download action on profile picture of everyone. Just create a folder in the main directory where your python script is saved, named ‘images’.

import urllib2
for f in r[‘data’]:
p_url = str(f[‘picture’][‘data’][‘url’])
urlopener1 = urllib2.build_opener()
page1 = opener1.open(p_url)
my_picture = page1.read()filename = f[‘name’]+”_”+f[‘id’]+”.jpg”
print filename+” downloaded…”
fout = open(‘images/’+filename, “wb”)
fout.write(my_picture)
fout.close()

Now you can check your images folder. It will have all the downloaded profile pictures of your friends.

That’s it. It may seem to be complicated at first, but you can open Graph API explorer at any point of time and understand how things are working.

Here is the Github Repo where I have committed the entire code that I used to download profile pics of my facebook friends. You can fork it and play around it as you want.

For feedback/suggestions/feature-requests, tweet me @sahildua2305 #FBtrick

Advertisements

3 comments

  1. Pingback: My timepass when SPOJ server was down! | Sahil Dua (sahildua2305)
  2. Julia · August 24, 2015

    Hi!
    Did you manage to get a list of everyone or just people who gave permissions to the facebook graph app? I tried the code you had, but only got a list of about 30 friends while I have over 500.
    Thanks!

    Like

    • sahildua2305 · August 25, 2015

      Hi Julia,

      Facebook has recently made changes in their API and now it returns results for only those Facebook friends who have used graph API earlier at some point of time.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s