[C#/Facebook API] 抓取粉絲專頁的塗鴉牆訊息
前言
之前已介紹過[C#/Facebook API] 利用Facebook API 發文,適合前後端平台(使用者無需輸入帳密方式)
這次再來看看以程式方式如何取得粉絲專頁的塗鴉牆訊息
(※寫這篇之前,facebook的社團和粉絲專頁我自己也傻傻分不清XD,寫文章分享順便釐清觀念是好事 )
實作
1. 先到Home - Facebook 開發人員填寫App資料,步驟可參考此篇:[C#/Facebook API] 利用Facebook API 發文,適合前後端平台(使用者無需輸入帳密方式)
2. 在Facebook Developer頁面中,不用為App加入任何存取權限,只要抄下AppID和AppSecret就好,待會程式要用到
3. 打開Visual Studio 2012,透過NuGet管理員加入Facebook C# SDK,不知道怎麼加的,參考上一篇文章::[C#/Facebook API] 利用Facebook API 發文,適合前後端平台(使用者無需輸入帳密方式)
4. 人工確認要抓資料的粉絲團Fan Page ID,以twMVC為例,網址原本為:https://www.facebook.com/twmvc
把www改成「graph」變成:https://graph.facebook.com/twmvc/ 進入後
下圖紅框處的id就是Fan Page ID,記下來,待會程式會用到

5. 程式代碼如下:
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Facebook;
namespace ConsoleApplicationFacebook
{
class Program
{
static void Main(string[] args)
{
string strAppID = "";
string strAppSecret = "";
//twMVC的Fan Page ID
string strFanPageID = "283092135094363";
WebClient wc = new WebClient();
string strResult = wc.DownloadString("https://graph.facebook.com/oauth/access_token?client_id=" + strAppID + "&client_secret=" + strAppSecret + "&grant_type=client_credentials");
string strAppaccessToken = strResult.Split('=')[1];//取得Access Token
Facebook.FacebookClient client = new FacebookClient(strAppaccessToken);
//回傳結果為json字串
string strJson = client.Get(strFanPageID + "/feed").ToString();
//輸出結果
Console.Write(strJson);
Console.ReadKey();
}
}
}
結果:

參考文章:
How to Get a Facebook fan page ID