2012/12/02

C# Facebook動態牆發文

今天突然看到[C#/Facebook API] 利用Facebook API 發文,適合前後端平台(使用者無需輸入帳密方式)這篇文章,覺得還不錯就將方法整合起來。

做出來的程式不要讓你朋友拿到,拿到的話你的動態牆可能會出現一些雜七雜八的東西…,或許會有謎片…記得要分享啊XD

使用前:



使用後:





Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Net;
using System.Resources;
using System.Text;
using System.Windows.Forms;
using Facebook;

namespace Facebook
{
    public partial class Form1 : Form
    {
        private static String AppID = "your AppID";
        private static String AppSecret = "your AppSecret";
        private static String UserId = "your UserID";
        private static WebClient wc = new WebClient();
        private static String result = wc.DownloadString("https://graph.facebook.com/oauth/access_token?client_id=" + AppID + "&client_secret=" + AppSecret + "&grant_type=client_credentials");
        private static String access_token = result.Split('=')[1];
        private static int num = 0;
        private Facebook.FacebookClient client = new FacebookClient(access_token);
        private static int[,] size = new int[2, 4] { { 310, 395, 255, 355 }, { 565, 395, 505, 355 } };

        public Form1()
        {
            InitializeComponent();
        }

        private void post_Message()
        {
            try
            {
                if (num == 0)   //如果是靜態訊息
                {
                    client.Post(UserId + "/feed", new { message = tb_Message.Text.ToString() });

                    MessageBox.Show(@"發佈到動態牆了!");
                }
                else if (num == 1) //如果是動態訊息
                {
                    dynamic messagePost = new ExpandoObject();

                    if (!tb_ImageUri.Text.Equals("") && !tb_UrlText.Text.Equals(""))
                    {
                        MessageBox.Show(@"請輸入正確的圖片連結以及超連結網址!");
                    }
                    else
                    {
                        messagePost.picture = tb_ImageUri.Text;//縮圖Url
                        messagePost.link = tb_Url.Text;//超連結
                        messagePost.name = tb_UriTitle.Text; //超連結文字
                        messagePost.caption = tb_UrlText.Text;//超連結描述
                        messagePost.description = tb_ImageText.Text;//圖片描述
                        messagePost.message = tb_Message2.Text; //發文內容
                        client.Post(UserId + "/feed", messagePost); //發佈
                    }
                }
            }
            catch (FacebookOAuthException ex)
            {
                MessageBox.Show(@"請輸入正確內容!");
            }
        }

        private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            num = e.TabPageIndex;   //取得目前的tabNumber
            setSize();  //設定表單大小
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = MyResource.Title;
            this.btn_Post.Text = MyResource.btn_Post;
            this.btn_Post1.Text = MyResource.btn_Post;
        }

        private void btn_Post_Click(object sender, EventArgs e)
        {
            post_Message();
        }

        private void setSize()
        {
            this.Height = size[num, 0];
            this.Width = size[num, 1];
            this.tabControl1.Height = size[num, 2];
            this.tabControl1.Width = size[num, 3];
        }
    }
}


參考文章:
http://www.dotblogs.com.tw/shadow/archive/2012/11/22/84963.aspx
http://www.dotblogs.com.tw/yc421206/archive/2009/11/04/11398.aspx
http://www.dotblogs.com.tw/johnny/archive/2010/01/20/13148.aspx