iam assuming You have already Registered Your Application with facebook and got client id
First place Two buttons For Authorization and Posting on webform with labels on web form
In authorization button click Event Write the following code
We set Offline access ,Publish stream Permission of users to post on walls.
protected void Authorization_Click(object sender, EventArgs e)
{
// Your Website Url which needs to Redirected
string callbackUrl = "xxxxxxx";
Response.Redirect(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope=offline_access,publish_stream,read_stream,publish_actions", ConfigurationManager.AppSettings["FacebookClientId"], callbackUrl));
}
After user Authorizes U will Receive Oauth code.use the below code to get Access token For that user
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&
client_secret=YOUR_APP_SECRET&code=The code U got from face book after Redirecting
This will Return Access token save it For Further use
Now its time For posting .place the below code in post button click event
//Paste the Following variables
public string Message = "";
public string AccessToken = "";
public string FacebookProfileID = "";
public string ErrorMessage { get; private set; }
public string PostID { get; private set; }
protected void BtnPublish_Click(object sender, EventArgs e)
{
postonwall();
}
//Method used To post Feed
public static string Append(string key, string value)
{
return string.Format("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value));
}
public void Post()
{
var url = "https://graph.facebook.com/me/feed?" +
Append("access_token", this.AccessToken);
var parameters = ""
+ Append("name", "name")
+ Append("link", "http://link.com")
+ Append("caption", "a test caption")
+ Append("description", "a test description")
+ Append("privacy", "{\"value\": \"EVERYONE\"}")
+ Append("message", this.Message);
var webRequest = WebRequest.Create(url);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters);
webRequest.ContentLength = bytes.Length;
System.IO.Stream os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
// Send the request to Facebook, and query the result to get the confirmation code
try
{
var webResponse = webRequest.GetResponse();
StreamReader sr = null;
try
{
sr = new StreamReader(webResponse.GetResponseStream());
this.PostID = sr.ReadToEnd();
}
finally
{
if (sr != null) sr.Close();
}
}
catch (WebException ex)
{
StreamReader errorStream = null;
try
{
errorStream = new StreamReader(ex.Response.GetResponseStream());
this.ErrorMessage = errorStream.ReadToEnd();
}
finally
{
if (errorStream != null) errorStream.Close();
}
}
}
public void postonwall()
{
this.Message = "Test message ";
this.ArticleTitle = "New Arcticle";
this.AccessToken = "xxxxxxxx";
this.Post();
}
The above code Returns post id if posted successfully.
Note: Access tokens are different For Different users
That’s it We posted a new post on users wall
Happy coding
First place Two buttons For Authorization and Posting on webform with labels on web form
In authorization button click Event Write the following code
We set Offline access ,Publish stream Permission of users to post on walls.
protected void Authorization_Click(object sender, EventArgs e)
{
// Your Website Url which needs to Redirected
string callbackUrl = "xxxxxxx";
Response.Redirect(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope=offline_access,publish_stream,read_stream,publish_actions", ConfigurationManager.AppSettings["FacebookClientId"], callbackUrl));
}
After user Authorizes U will Receive Oauth code.use the below code to get Access token For that user
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&
client_secret=YOUR_APP_SECRET&code=The code U got from face book after Redirecting
This will Return Access token save it For Further use
Now its time For posting .place the below code in post button click event
//Paste the Following variables
public string Message = "";
public string AccessToken = "";
public string FacebookProfileID = "";
public string ErrorMessage { get; private set; }
public string PostID { get; private set; }
protected void BtnPublish_Click(object sender, EventArgs e)
{
postonwall();
}
//Method used To post Feed
public static string Append(string key, string value)
{
return string.Format("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value));
}
public void Post()
{
var url = "https://graph.facebook.com/me/feed?" +
Append("access_token", this.AccessToken);
var parameters = ""
+ Append("name", "name")
+ Append("link", "http://link.com")
+ Append("caption", "a test caption")
+ Append("description", "a test description")
+ Append("privacy", "{\"value\": \"EVERYONE\"}")
+ Append("message", this.Message);
var webRequest = WebRequest.Create(url);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters);
webRequest.ContentLength = bytes.Length;
System.IO.Stream os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
// Send the request to Facebook, and query the result to get the confirmation code
try
{
var webResponse = webRequest.GetResponse();
StreamReader sr = null;
try
{
sr = new StreamReader(webResponse.GetResponseStream());
this.PostID = sr.ReadToEnd();
}
finally
{
if (sr != null) sr.Close();
}
}
catch (WebException ex)
{
StreamReader errorStream = null;
try
{
errorStream = new StreamReader(ex.Response.GetResponseStream());
this.ErrorMessage = errorStream.ReadToEnd();
}
finally
{
if (errorStream != null) errorStream.Close();
}
}
}
public void postonwall()
{
this.Message = "Test message ";
this.ArticleTitle = "New Arcticle";
this.AccessToken = "xxxxxxxx";
this.Post();
}
The above code Returns post id if posted successfully.
Note: Access tokens are different For Different users
That’s it We posted a new post on users wall
Happy coding
No comments:
Post a Comment