Saturday, October 19, 2013

Read-out Facebook Friend List

Notice: Since the release of the Facebook API 2.0 in April 2014 the chat function is disabled, which means that this code unfortunately is no longer runnable.

One could request the Facebook friend list with the Graph API, similiar to the principle in this post. For that though one needs an access token, which expires from time to time and has to work with JSON objects.
Therefore I use a function of the XMPP protocoll. A requirement for this is a connection to the Facebook chat, like I described in the previous post.
Now we just have to add a handler for the OnRosterItem event, which occurs once for every contact in the contact list, so we can read-out all friends.
We can do this for example on login:

private void OnLogin(object sender)
{
    xmpp.OnRosterItem += new XmppClientConnection.RosterHandler(xmpp_OnRosterItem);
}

For saving the friends a Dictionary is suited:

Dictionary<string, string> Friends = new Dictionary<string, string>();

In the handling of OnRosterItem we then save all contacts in the dictionary:

private void xmpp_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
{
    Friends.Add(item.GetAttribute("jid").ToString(), item.GetAttribute("name").ToString());
}

For sending / receiving messages we need the Jid of the partner, which is why we save that, we use the name for converting it to the known name.
Jid and name can then be accessed as follows:

string JidBloggeroliver = Friends.First(x => x.Value == "Olli Michael").Key;
string NameBloggeroliver = Friends["-100006476847029@chat.facebook.com"];

Now the complete code. I added a loop after opening the connection, which waits, until all contacts are loaded.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.IO;

using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.protocol.iq.roster;
using agsXMPP.Collections;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        XmppClientConnection xmpp = new XmppClientConnection("chat.facebook.com");
        bool StartCollectingContacts = false;
        bool CollectingContacts = true;
        Dictionary<string, string> Friends = new Dictionary<string, string>();

        private void Form1_Load(object sender, EventArgs e)
        {
            xmpp.OnLogin += new ObjectHandler(OnLogin);
            xmpp.Open("name", "password");

            while (CollectingContacts)
            {
                if (StartCollectingContacts)
                    CollectingContacts = false;
                System.Threading.Thread.Sleep(1000);
            }

            string JidBloggeroliver = Friends.First(x => x.Value == "Olli Michael").Key;
            string NameBloggeroliver = Friends["-100006476847029@chat.facebook.com"];
        }

        private void OnLogin(object sender)
        {
            xmpp.OnRosterItem += new XmppClientConnection.RosterHandler(xmpp_OnRosterItem);
        }

        private void xmpp_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
        {
            StartCollectingContacts = true;
            CollectingContacts = true;
            Friends.Add(item.GetAttribute("jid").ToString(), item.GetAttribute("name").ToString());
        }
    }

}

No comments:

Post a Comment