Tuesday 21 March 2017

Posting Image and Text in Facebook

Posting Image and Text in Facebook:

For Posting images and text messages in Facebook, you have to follow these steps:

Step-1:

Create a new ASP.Net Solution and Add a Default.aspx page. Then copy its localhost url.

Step-2:

Log in to your Facebook account. Then Open this given link https://developers.facebook.com/apps.

Step-3:


Click on “Add a new App”. Then a popup window will open.


Step-4:

In this popup, write your Application Name in Display Name and Select Category. Then Click on Create App ID. Then it will ask for security check. Then Submit it.


Step-5:

After Submit a developer Application window will open. And go to the settings page.


Then Copy the App ID and App Secret for your solution.

Step-6:

Then click on Add Platform and choose website. And set your website or localhost url.


Step-7:

Go to your menu bar option, select Add Product and then choose Facebook login option.



Step-8:

Now go to the Facebook Login Page. And give your website url or localhost url in Valid OAuth redirect URIs. Then click on save changes


Step-9:

Then go to the App Review page. Then set your created App public true.


Then click on start a submission button.


Then select all items from this submission page and click add items.


Step-10:

Now you go to your ASP.Net solution. And a “aspsnippets.facebookapi.dll” file in your solution reference.

Step-11:

Go to your Default.aspx page and add controls in your design page.

<asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <br />
    <asp:TextBox ID="txtMessage" runat="server" TextMode = "MultiLine"></asp:TextBox>
    <hr />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick = "UploadPhoto" />
 

Step-12:

Write this below coding in your “Default.aspx.cs” page:
1.Add Namespace:
using ASPSnippets.FaceBookAPI;


2. Write this below coding in “Page_Load” event.  

        FaceBookConnect.API_Key = "404417453247103";
        FaceBookConnect.API_Secret = "03c91fd2103207adb84508249e6848dd";
        if (!IsPostBack)
        {
            HttpPostedFile updfile=(HttpPostedFile)Session["File"];
            string code = Request.QueryString["code"];
            if (!string.IsNullOrEmpty(code))
            {
                if (updfile.FileName != "")
                {
                    FaceBookConnect.PostFile(code, "me/photos", updfile, Session["Message"].ToString());
                    Session["File"] = null;
                    Session["Message"] = null;
                }
                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;
                }
            }
        }
   

3.Write these code in button click event:

        Session["File"] = FileUpload1.PostedFile!=null?FileUpload1.PostedFile:null;
        Session["Message"] = txtMessage.Text;
        FaceBookConnect.Authorize("user_photos,publish_actions", Request.Url.AbsoluteUri.Split('?')[0]);
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Image has been shared.');", true);
    

3 comments:

  1. FB now require a Privacy URL before an app can go live.

    ReplyDelete
  2. With code and App settings very similar to above (except not a public application) to that shown I get a C# error when executing FaceBookConnect.PostFile saying it requires a User Access token.

    ReplyDelete
  3. After making application public I get an the error 'Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.' when executing the PostFile statement

    ReplyDelete