In this article I will explain how to post publish status message, link, video or image to FaceBook Wall using FaceBook Graph API and Free ASP Snippets FaceBook API in ASP.Net.
Create FaceBook Application and get App Id
You will need to create an application and get an API Key and API Secret.
HTML Markup
The HTML Markup is very simple. There’s an ASP.Net Button btnConnect to authorize the FaceBook user and get the access token, a multiline ASP.Net TextBox txtMessage to allow user type the message to be published on the Facebook wall and finally another ASP.Net Button to trigger the process.
<asp:Button ID="btnAuthorize" runat="server" Text="Authorize" OnClick="Authorize" /><hr />
<asp:Panel ID="pnlPost" runat="server" Enabled="false">
Message: <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"></asp:TextBox><br />
<asp:Button ID="btnPost" runat="server" Text="Post to Wall" OnClick="Post" />
</asp:Panel>
Namespaces
You will need to import the following Namespaces.
C#
using ASPSnippets.FaceBookAPI;
using System.Collections.Generic;
VB.Net
Imports ASPSnippets.FaceBookAPI
Imports System.Collections.Generic
Authorize the User using FaceBook Graph API
Firstly we will need to authorize the user using the FaceBook Graph API and get the Access Token so that we can post on the user’s FaceBook wall.
In the below code we are authorizing the user using FaceBook and also asking him to provide permission to the application to post on the user’s FaceBook wall.
C#
protected void Page_Load(object sender, EventArgs e)
{
FaceBookConnect.API_Key = "<Your API Key>";
FaceBookConnect.API_Secret = "<Your API Secret>";
if (!IsPostBack)
{
string code = Request.QueryString["code"];
if (!string.IsNullOrEmpty(code))
{
ViewState["Code"] = code;
pnlPost.Enabled = true;
btnAuthorize.Enabled = false;
}
}
}
protected void Authorize(object sender, EventArgs e)
{
FaceBookConnect.Authorize("publish_actions", Request.Url.AbsoluteUri);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
FaceBookConnect.API_Key = "<Your API Key>"
FaceBookConnect.API_Secret = "<Your API Secret>"
If Not IsPostBack Then
Dim code As String = Request.QueryString("code")
If Not String.IsNullOrEmpty(code) Then
ViewState("Code") = code
pnlPost.Enabled = True
btnAuthorize.Enabled = False
End If
End If
End Sub
Protected Sub Authorize(ByVal sender As Object, ByVal e As EventArgs)
FaceBookConnect.Authorize("publish_actions", Request.Url.AbsoluteUri)
End Sub
The below screenshot displays the FaceBook page asking the user to provide permission to the application to publish content on user’s wall.
Share, publish and post content on FaceBook wall
Below is the code to post the content onto the user’s FaceBook Wall.
C#
protected void Post(object sender, EventArgs e)
{
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("message", txtMessage.Text);
FaceBookConnect.Post(ViewState["Code"].ToString(), "me/feed", data);
Response.Redirect("http://www.facebook.com/me/");
}
VB.Net
Protected Sub Post(ByVal sender As Object, ByVal e As EventArgs)
Dim data As Dictionary(Of String, String) = New Dictionary(Of String, String)
data.Add("message", txtMessage.Text)
FaceBookConnect.Post(ViewState("Code").ToString, "me/feed", data)
Response.Redirect("http://www.facebook.com/me/")
End Sub
The above code posts a simple Text Message as shown in the below screenshot.
In order to post a message with image, link and message you need to do as following.
C#
protected void Post(object sender, EventArgs e)
{
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("link", "http://www.aspsnippets.com");
data.Add("picture", "//www.aspsnippets.com/images/Blue/Logo.png");
data.Add("caption", "ASP Snippets");
data.Add("name", "ASP Snippets");
data.Add("message", "I like ASP Snippets");
FaceBookConnect.Post(ViewState["Code"].ToString(), "me/feed", data);
}
VB.Net
Protected Sub Post(ByVal sender As Object, ByVal e As EventArgs)
Dim data As Dictionary(Of String, String) = New Dictionary(Of String, String)
data.Add("link", "http://www.aspsnippets.com")
data.Add("picture", "//www.aspsnippets.com/images/Blue/Logo.png")
data.Add("caption", "ASP Snippets")
data.Add("name", "ASP Snippets")
data.Add("message", "I like ASP Snippets")
FaceBookConnect.Post(ViewState("Code").ToString, "me/feed", data)
End Sub
The below screenshot displays a post with image, link and message.
Demo
Downloads
You can download the complete source code in VB.Net and C# along with the ASPSnippets.FaceBookAPI DLL using the download link provided below.