To post status on Facebook using ASP.NET, we must have a valid Facebook account. We need to create a Facebook App first to get App Id and Secret Key.
Steps to create Facebook App to receive App Id and Secret Key
In order to get an App Id and Secret key we need to register a new application
Visit Facebook Developer Page as in the following screenshot:
Register yourself as Facebook Developer.
After registration as Facebook Developer,
Click Register:
Create New App,
We will choose Website as a platform to get started and thereby App Id and Secret Key will be generated.
After selecting Website as a platform, the following screen appears. In the top right, look for the button “Skip and Create App ID” and click it.
Clicking the “Skip and Create App ID” button will bring up the following screen.
You need to fill out Display Name and select a Category. Here we gave “Traffic Cop” display name.
You can ignore the Namespace field as this is not necessary for our purpose. Select the category of the app. Here we used News as the category of the app.
After following above steps, we will be redirected to the following screen. On this screen we can see App Id and App Secret in two fields at the top. App Secret is in password format. Click the “Show” button, this will display App Secret Key.
After clicking Show button, we can see the App Secret Key thereby can use in in our application.
These Facebook App Id and App Secret Key is used in the application from where we wish to make Facebook post.
Now set the Site Url, for this go to Settings add the Site URL.
After applying App Id and App Secret, we can now post on Facebook wall.
Let's say, we have the following front end screen.
Front-End
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <link href="bootstrap.min.css" rel="stylesheet" />
- </head>
- <body>
- <form id="form1" runat="server" style="margin-top:40px;">
- <div>
-
- <asp:FileUpload ID="FileUpload1" runat="server" style="margin-left: 538px;" />
- <br />
- <br />
-
- <asp:TextBox ID="txtMessage" runat="server" TextMode = "MultiLine" CssClass="col-xs-4" style="margin-left: 538px;" ></asp:TextBox>
- <br />
- <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick = "UploadPhoto" style="margin-left: 538px;" CssClass="btn" />
- </div>
- </form>
- </body>
- </html>
User can choose image file and add caption to the same and upload it to the Facebook wall using Facebook App.
Backend Snippet
- public partial class CS : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- FaceBookConnect.API_Key = "enter App Id here";
- FaceBookConnect.API_Secret = "enter App Secret here";
- if (!IsPostBack)
- {
-
- string code = Request.QueryString["code"];
- if (!string.IsNullOrEmpty(code))
- {
-
- if (Session["File"] != null)
- {
- FaceBookConnect.PostFile(code, "me/photos", (HttpPostedFile)Session["File"], Session["Message"].ToString());
- }
- else
- {
- Dictionary<string, string> data = new Dictionary<string, string>();
- data.Add("message", Session["Message"].ToString());
- FaceBookConnect.Post(code, "me/feed", data);
- }
-
-
- Session["File"] = null;
- Session["Message"] = null;
- }
- }
- }
-
- protected void UploadPhoto(object sender, EventArgs e)
- {
- if (FileUpload1.HasFile)
- Session["File"] = FileUpload1.PostedFile;
- Session["Message"] = txtMessage.Text;
- FaceBookConnect.Authorize("user_photos,publish_actions", Request.Url.AbsoluteUri.Split('?')[0]);
- }
- }
We can see that photo is uploaded on the Facebook profile with the caption.
Closure
In this way, we implemented a demo how we can post on Facebook using App Id and App Secret account of Facebook App. Hope you like it.
Read more articles on ASP.NET: