Software & Finance





Facebook Apps - Programmatically post to all of your friend's wall





Here is the sample code in C# that would post to all of your friends programmatically.

 

It is a simple C# console application and you need to get the access token using the link https://developers.facebook.com/tools/explorer/

 

NOTE: The code would work fine, but facebook might restrict you stating that maximum number of posts reached for the day.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace FriendFeedConsole
{
   class Program
   {
      static void Main(string[] args)
      {
         try
         {
            string access_token = "your access token"; // With publish_stream rights

            string URL = "https://graph.facebook.com/me/friends&";
            URL += "access_token=" + access_token;

            WebClient webClient = new WebClient();

            Stream rstream = webClient.OpenRead(URL);
            StreamReader objReader = new StreamReader(rstream);

            string sLine = "";
            string result = "";

            while (sLine != null)
            {
               sLine = objReader.ReadLine();
               result += sLine;
               result += "\n";
            }
            webClient.Dispose();

            Newtonsoft.Json.Linq.JObject myFriends = Newtonsoft.Json.Linq.JObject.Parse(result);
            Newtonsoft.Json.Linq.JArray arrFriends = (Newtonsoft.Json.Linq.JArray)myFriends["data"];

            for (int i = 0; i < arrFriends.Count; i++)
            {
               Newtonsoft.Json.Linq.JToken token = arrFriends[i];

               string id = token["id"].ToString().Replace("\"", "");
               string name = token["name"].ToString().Replace("\"", "");
               //result = result + "
" + "id: " + id + " name: " + name + ""; string message = "Wish you Happy New Year 2012! Visit us at http://www.softwareandfinance.com"; if (message != null && message.Length > 0) { string output = name + " wallpost id: " + postedonwall(id, access_token, message) + "\n"; Console.WriteLine(output); } } } catch (WebException ex) { if (ex.Response is HttpWebResponse) { switch (((HttpWebResponse)ex.Response).StatusCode) { case HttpStatusCode.NotFound: Console.WriteLine("Not Found Exception"); break; default: Console.WriteLine(ex.Message + ex.InnerException); break; } } } catch (Exception e) { Console.WriteLine("Unknown Exception"); } } static public string postedonwall(string uid, string access_token, string message) { try { string URL = "https://graph.facebook.com/" + uid + "/feed"; NameValueCollection formData = new NameValueCollection(); formData["access_token"] = access_token; formData["message"] = message; WebClient webClient = new WebClient(); byte[] responseBytes = webClient.UploadValues(URL, "POST", formData); string result = Encoding.UTF8.GetString(responseBytes); webClient.Dispose(); return result; } catch (System.Exception ex) { return "Unable to post: " + ex.Message; } } } }