Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using facebook api wnt to post messages to other one



Is it posible


Regards
Chetan Chaudhari
Posted

 
Share this answer
 
Comments
ambarishtv 1-Jun-11 4:49am    
my 5
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Facebook;
using Facebook.Web;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Utility;
using Facebook.Winforms;
using Facebook.Session;
using System.Collections.Generic;
using Facebook.Winforms.Components;
using System.Web.UI.MobileControls;
using System.Net;
using System.IO;
using System.Text;
namespace FacebookDemo
{
public partial class index : System.Web.UI.Page
{
//K.Durga Nageswara Rao..............//

private const string APPID = "Your App Id";
private const string APPKEY = "Your App Key"; // From the Facebook application page
private const string SECRET = "Your App Secreat";
////Facebook.Web.FbmlControls.PromptPermission prompter = new Facebook.Web.FbmlControls.PromptPermission();
//Facebook.Session.ConnectSession session = new Facebook.Session.ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);

//protected void Page_Load(object sender, EventArgs e)
//{

//string sessionKey = Session["facebook_session_key"] as String;
//string userId = Session["facebook_userId"] as string;

//string authToken = Request.QueryString["auth_token"];
//string accesstoken = Request.QueryString["code"];


//if (!String.IsNullOrEmpty(sessionKey))
//{
// session.SessionKey = sessionKey;
// //session.UserId = userId;
//}
//else if (!String.IsNullOrEmpty(authToken))
//{
// Session["facebook_session_key"] = session.SessionKey;
// Session["facebook_userId"] = session.UserId;
// Session["facebook_session_expires"] = session.SessionExpires;
//}
//else
//{
// Response.Redirect(@"http://www.facebook.com/login.php?api_key=" + APPKEY + @"&v=1.0");
//}
//if (ConnectAuthentication.isConnected())
//{
// // Facebook.Session.ConnectSession session = new Facebook.Session.ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);
// string sessionkey = session.SessionKey;
// string UserId = session.UserId.ToString();
// //session.UserId = ConnectAuthentication.UserID;
// Facebook.Rest.Api api = new Facebook.Rest.Api(session);
// //Display user data captured from the Facebook API.
// Facebook.Schema.user user = api.Users.GetInfo();
// string fullName = user.first_name + " " + user.last_name;
// Panel1.Visible = true;
// Label1.Text = fullName;
//}
//else
//{
// //Facebook Connect not authenticated, proceed as usual.
//}



Facebook.Web.FbmlControls.PromptPermission prompter = new Facebook.Web.FbmlControls.PromptPermission();
protected void Page_Load(object sender, EventArgs e)
{
string authToken = Request.QueryString["auth_token"];

if (ConnectAuthentication.isConnected())
{
Facebook.Session.ConnectSession session = new Facebook.Session.ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);
// session.UserId = ConnectAuthentication.UserID;
Facebook.Rest.Api api = new Facebook.Rest.Api(session);

//Display user data captured from the Facebook API.

Facebook.Schema.user user = api.Users.GetInfo();
string fullName = user.first_name + " " + user.last_name;
Panel1.Visible = true;
Label1.Text = fullName;
Facebook.Schema.pages_getInfo_response pageinfo = new pages_getInfo_response();


System.Web.HttpContext.Current.Session["sessionkey"] = session.SessionKey;
System.Web.HttpContext.Current.Session["UserId"] = session.UserId;
System.Web.HttpContext.Current.Session["AccessToken"] = session.SessionSecret;
//var val = HttpContext.Current.Request.Cookies["b90e89f2fcf265f26708476a186b1222_user"].Values;//100002386562731
//var val0 = HttpContext.Current.Request.Cookies["b90e89f2fcf265f26708476a186b1222"].Values;//4febeb45d23f49fa9d793c29da703336
//var val1 = HttpContext.Current.Request.Cookies["b90e89f2fcf265f26708476a186b1222_ss"].Values;//AQA9rwe7RanXzDeV
//var val2 = HttpContext.Current.Request.Cookies["b90e89f2fcf265f26708476a186b1222_session_key"].Values;//2.AQD709Yz-kp5xWcg.3600.1306418400.1-100002386562731
//var val3 = HttpContext.Current.Request.Cookies["b90e89f2fcf265f26708476a186b1222_expires"].Values;//1306418400
}
else
{
if(string.IsNullOrEmpty(ConnectAuthentication.SessionKey))
{
//Page_Load(sender,e);
}
// Response.Redirect(@"http://www.facebook.com/login.php?api_key=" + APPKEY + @"&v=1.0");

}

//postToWall();
//string signedRequest = Request.Form["signed_request"];
//var DecodedSignedRequest = FacebookSignedRequest.Parse(FacebookContext.Current.AppSecret, SignedRequest);
//dynamic SignedRequestData = DecodedSignedRequest.Data;
//var RawRequestData = (IDictionary<string, object>)SignedRequestData;
//if (RawRequestData.ContainsKey("page") == true)
//{ Facebook.JsonObject RawPageData = (Facebook.JsonObject)RawRequestData["page"];
// if (RawPageData.ContainsKey("id") == true)
// currentFacebookPageID = (string)RawPageData["id"];
//}
}
protected void Button1_Click(object sender, EventArgs e)
{
Facebook.Session.ConnectSession session1 = new Facebook.Session.ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);
if (ConnectAuthentication.isConnected())
{
//ConnectAuthentication.UserID = System.Web.HttpContext.Current.Session["UserId"];
FacebookService fbService = new FacebookService();
//fbService.SessionKey = session1.SessionKey;
fbService.uid = session1.UserId;
//fbService.ApplicationKey = ConnectAuthentication.ApiKey;

attachment attach = new attachment();

//attach.caption = "Check this out...";
//attach.description = "Posting from my Facebook development App";
//attach.href = "http://www.kennetham.com/";
//attach.name = "Attach my web link via FacebookAPI";
attachment_media attach_media = new attachment_media();
attach_media.type = attachment_media_type.image;
attachment_media_image image = new attachment_media_image();
image.type = attachment_media_type.image;
image.href = "http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/yo/r/UlIqmHJn-SK.gif";
image.src = "http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/yo/r/UlIqmHJn-SK.gif";
List<attachment_media> attach_media_list = new List<attachment_media>();
attach_media_list.Add(image);
attach.media = attach_media_list;
//attachment_property attach_prop = new attachment_property();
///* action links */
//List<action_link> actionlink = new List<action_link>();
//action_link al1 = new action_link();
//al1.href = "http://www.kennetham.com/";
//al1.text = "Kenneth Ham";
//actionlink.Add(al1);
Facebook.Session.ConnectSession session = new Facebook.Session.ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);
//session.UserId = ConnectAuthentication.UserID;
Facebook.Rest.Api api = new Facebook.Rest.Api(session);
//Display user data captured from the Facebook API.
string publishAccess = api.ExtendedPermissionUrl(Facebook.Schema.Enums.ExtendedPermissions.publish_stream);
Uri uri= new Uri(String.Format("https://graph.facebook.com/189415601106529/feed?api_key={0}&v=1.0&ext_perm={1}&message=uuuuhhhuuu...&method=post", ConnectAuthentication.ApiKey, publishAccess));
Response.Redirect(uri.ToString());
// api.Stream.Publish(TextBox1.Text, attach, actionlink, fbService.uid.ToString(), 0);
api.Stream.Publish(TextBox1.Text, null, null, fbService.uid.ToString(), 0);



}
else
{
//Facebook Connect not authenticated, proceed as usual.
}
}




private static Uri GetExtendedPermissionUrl(Enums.ExtendedPermissions permission)
{
return new Uri(String.Format("http://www.facebook.com/authorize.php?api_key={0}&v=1.0&ext_perm={1}", ConnectAuthentication.ApiKey, permission));
//return new Uri(String.Format("https://graph.facebook.com/189415601106529/feed?api_key={0}&v=1.0&ext_perm={1}&message=uuuuhhhuuu...&method=post", ConnectAuthentication.ApiKey, permission));

//https://graph.facebook.com/<Page_ID>/feed?access_token=<PAGE_ACCESS_TOKEN_TAKEN_FROM_STEP2>&message=uuuuhhhuuu...
}
protected void Button2_Click(object sender, EventArgs e)
{
Enums.ExtendedPermissions perm;
perm = Enums.ExtendedPermissions.publish_stream;

Uri uri = GetExtendedPermissionUrl(perm);
Response.Redirect(uri.ToString());

}







protected void Button3_Click(object sender, EventArgs e)
{
//Facebook.Session.ConnectSession session1 = new Facebook.Session.ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);
//this.Session.Remove("USER_ID");
//this.Session.Remove("FACEBOOK_USER_INFO");
//try
//{
// if (ConnectAuthentication.SessionKey!= null)
// {
// if (ConnectAuthentication.isConnected())
// {
// this.Session.Clear();
// } // end if
// } // end if
//}
//catch (Exception ex)
//{
//} // end catch
//this.Response.Redirect("~/index.aspx", true);
//to post message to fan pages...........
//dynamic messagePost = new ExpandoObject();
//messagePost.access_token = "[YOUR_ACCESS_TOKEN]";
//messagePost.picture = "[A_PICTURE]";
//messagePost.link = "[SOME_LINK]"; messagePost.name = "[SOME_NAME]";
//messagePost.caption = "{*actor*} " + "[YOUR_MESSAGE]";
////<---{*actor*} is the user (i.e.: Aaron)
//messagePost.description = "[SOME_DESCRIPTION]";
//FacebookClient app = new FacebookClient("[YOUR_ACCESS_TOKEN]");
//try {
// var result = app.Post("/" + [PAGE_ID] + "/feed", messagePost);
//} catch (FacebookOAuthException ex)
//{
// //handle something
//}
}


private IList<page> GetInfo(List<string> fields, List<long> page_ids, long? uid, bool isAsync, Facebook.Rest.Pages.GetInfoCallback callback, Object state)
{
var parameterList = new Dictionary<string, string> { { "method", "facebook.pages.getInfo" } };
Utilities.AddList(parameterList, "fields", fields);
Utilities.AddList(parameterList, "page_ids", page_ids);
Utilities.AddOptionalParameter(parameterList, "uid", uid);
parameterList["fields"] = parameterList["fields"].Replace("'", "");
if (isAsync)
{
SendRequestAsync<pages_getInfo_response, IList<page>>(parameterList, !string.IsNullOrEmpty(System.Web.HttpContext.Current.Session["sessionkey"].ToString()), new FacebookCallCompleted<IList<page>>(callback), state, "page");
return null;
}
var response = SendRequest<pages_getInfo_response>(parameterList, !string.IsNullOrEmpty(System.Web.HttpContext.Current.Session["sessionkey"].ToString()));
//return response == null ? null : response.page;
return null;
}
private object SendRequest<T1>(Dictionary<string, string> parameterList, bool p)
{
throw new NotImplementedException();
}
private void SendRequestAsync<T1, T2>(Dictionary<string, string> parameterList, bool p, FacebookCallCompleted<IList<page>> facebookCallCompleted, object state, string p_5)
{
throw new NotImplementedException();
}

protected void Button3_Click1(object sender, EventArgs e)
{
//var dicParams = new Dictionary<string, object>();
//dicParams["message"] = "hi, r u fine";
//dicParams["caption"] = string.Empty;
//dicParams["description"] = string.Empty;
//dicParams["name"] = "sample fan page message";
//dicParams["req_perms"] = "publish_stream";
//dicParams["scope"] = "publish_stream";
//long destinationID = 189415601106529;
//long accountID=Convert.ToInt64(System.Web.HttpContext.Current.Session["UserId"]);
//// Get the access token of the posting user if we need to
//if (destinationID != accountID)
//{
// dicParams["access_token"] = this.getPostingUserAuthToken(destinationID);
//}
//publishResponse = this.FacebookConnection.Post("/" + destinationID + "/feed", dicParams);
Response.RedirectLocation = "http://192.168.10.57/samplefacebook/index.aspx";
Response.Redirect("https://graph.facebook.com/me/accounts?access_token=<ACCESS_TOKEN>");

}


//protected string postToWall()
//{
// var accessToken = Session["_FB_ACCESS_TOKEN"];
// var graphId = Session["_FB_USER_GRAPH_ID"];
// var url = string.Format("https://graph.facebook.com/{0}/feed", graphId);
// var req = WebRequest.Create(url);
// req.Method = "POST";
// req.ContentType = "application/x-www-form-urlencoded";
// string postData = string.Format(@"curl -F 'access_token={0}' -F 'message=This is a test...' https://graph.facebook.com/{1}/feed", accessToken, graphId);
// byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// var stream = req.GetRequestStream();
// stream.Write(byteArray, 0, byteArray.Length);
// stream.Close();
// WebResponse response = req.GetResponse();
// Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// stream = response.GetResponseStream();
// StreamReader reader = new StreamReader(stream);
// return reader.ReadToEnd();
//}
}


}
 
Share this answer
 
Comments
aryan2010 16-Sep-11 3:01am    
What will be the design for this page and how user can login through face book connect.
Thanks,
Deepak

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900