2013/04/06

C#.Net 取得Facebook好友名單








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

namespace FacebookFriendList
{
    public partial class Form1 : Form
    {
        private String AccessToken = "Access Token";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count > 0)
            {
                listBox1.Items.Clear();
            }
            FacebookClient client = new FacebookClient(AccessToken);
            Object friendList = client.Get("/me/friends");
            JObject friendJson = JObject.Parse(friendList.ToString());

            Dictionary<String, String> fbList = new Dictionary<string, string>();
            foreach (Object str in friendJson["data"].Children())
            {
                String temp = str.ToString().Replace("{", "").Replace("}", "").Replace("\"", "").Replace("id:", "").Replace("\"", "");
                temp = temp.Substring(temp.IndexOf(":") + 1);
                String[] userInformation = temp.Split(',');
                fbList.Add(userInformation[1], userInformation[0]);
            }

            foreach (String name in fbList.Values)
            {
                listBox1.Items.Add(name);
            }
        }
    }
}







參考資料:
https://developers.facebook.com/docs/reference/api/FriendList/