Analyse Facebook Friends Geography (en-US/C#) [FOR FUN]

After talking with a friend of mine about our facebook friends, i decided to tell him a more precise information about where are my friends in the globe. In order to give him a response tomorrow i make a simple tool to give me that information.

What i am going to show you here is how i made it and maybe incourage you to check on your friends as well. First thing to do is study the facebook C# API :

Technologies used:

  • C#
  • Excel
  • Excel PowerView
  • Facebook C# API

After that, i started developing my mini-tool to connect to facebook. This is very simple and it only took me an hour to make it. This tool is a simple windows form app with a webbrowser and a datagrid. Once you login it will show you your friends info (for those with a valid location) and it will save a csv in you c:\temp folder.

Step 0 – Install the facebook api package in your solution:

NUGET: Install-Package Facebook

Step 1 – Register in Facebook App (


Step 2 – Start coding like a mad man (please do not take this code only as a reference)

It was actually a windows form app, really ugly but it made my purpose. The form class is here:

public partial class Form1 : Form
private string _facebookAppId = "your app id – provided by the facebook app registration";
private string _permissions = "user_about_me,user_location,friends_about_me,friends_location";
private FacebookClient _fb = new FacebookClient();
private List<ContactInfo> listaContactos = new List<ContactInfo>();

public Form1()

private void Form1_Load(object sender, EventArgs e)

var redirectUrl = "<a href=";;">";</a>

//fb.AppId = facebookAppId;
var loginUrl = _fb.GetLoginUrl(new
client_id = _facebookAppId,
redirect_uri = redirectUrl,
scope = _permissions,
display = "popup",
response_type = "token"

webBrowser1.Navigated += (s, ev) => {
FacebookOAuthResult result;
if (_fb.TryParseOAuthCallbackUrl(ev.Url, out result))
if (result.IsSuccess)
var accesstoken = result.AccessToken;

_fb.AccessToken = accesstoken;

var errorDescription = result.ErrorDescription;
var errorReason = result.ErrorReason;




public void GetAllFriendsInsertToSQL()

var me = _fb.Get("/me/friends");
var results = JObject.Parse(me.ToString());

foreach(var element in results)
var children = element.Value;

foreach (var child in children)
if (child.Next != null)
var id = child.Value<string>("id");

var name = child.Value<string>("name");

var friend = (Facebook.JsonObject)_fb.Get("/" + id);

object location;

friend.TryGetValue("location", out location);

if (location != null)
var loc = (Facebook.JsonObject)location;

object city;

loc.TryGetValue("name", out city);

if (city != null)
listaContactos.Add(new ContactInfo { Id = id, Name = name, City = city.ToString() });

dataGridView1.DataSource = listaContactos;


public void SaveToCSV()
string file=@"C:\Temp\facebookAPI.csv";


foreach (var item in listaContactos)

Once you run my code, you will find a form that looks like the following:


Once you sign in it will automatically start downloading your friends info. Notice in the code that i only save the info of my friends that have a location in its data, because my main goal is to check my friends distribution in the world.


Step 3 – Save to file

If you paid some atention to my code you could see that at the end i call a method to save my file as csv in c:\temp\FacebookAPI.csv. Once you have that file, open it in excel.


After you open it, add a header with names that identify each attributes, then format as a table.


Step 4- Add a pivot chart  – Select the entire table and the in excel select Insert->Pivot Table


Now you can start making your own analysis, for instance i have 2 friends in France and 1 in Belgium:


Step 5 – My Favourite (PowerView)

First save your file as XLSX


Then Go To PowerView Tab in Excel:


Now maintain the same attributes selected:


And finnaly for the expected result click on DESING –> MAP


Watch magic happening:


Now i can tell my friend the right answer.


By the way, if some one wants the too, ask me ( and i will send it. 


Thank you,

Rui Machado


Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da

Está a comentar usando a sua conta Terminar Sessão /  Alterar )

Google photo

Está a comentar usando a sua conta Google Terminar Sessão /  Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão /  Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão /  Alterar )

Connecting to %s