简体   繁体   中英

Post message from Website to Facebook wall

I have a website made with ASP.NET webform .NET 4.5 C#. This site contains a forum(homemade by me), parts of this forum needs to be posted to the specific facebook wall(made for this webpage). What I need :

  1. Post just created thread(message) from specific part of the forum to the corsponding facebook wall.

  2. Optional : Sync the forum thread on webpage with message/comments on the specific facebook page

I have looked at these guides : http://www.codeproject.com/Articles/569920/Publish-a-post-on-Facebook-wall-using-Graph-API http://www.c-sharpcorner.com/UploadFile/raj1979/post-on-facebook-users-wall-using-Asp-Net-C-Sharp/

But im not sure that this is really the solution for me? I have tried to follow the guide but it does not look the same.

Edit :

dynamic result;

        //https://developers.facebook.com/tools/explorer/
        //https://developers.facebook.com/tools/access_token/
        FacebookClient client = new FacebookClient(ConfigurationManager.AppSettings["FacebookAppToken"]);

        //result = client.Get("debug_token", new
        //{
        //    input_token = ConfigurationManager.AppSettings["FacebookAppToken"],
        //    access_token = ConfigurationManager.AppSettings["FacebookAppToken"]
        //});


        //result = client.Get("oauth/access_token", new
        //    {
        //        client_id = ConfigurationManager.AppSettings["FacebookAppId"],
        //        client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"],
        //        grant_type = "client_credentials",
        //        //redirect_uri = "http://www.MyDomain.net/",
        //        //code = ""
        //    });

        result = client.Get("oauth/access_token", new
        {
            client_id = ConfigurationManager.AppSettings["FacebookAppId"],
            client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"],
            grant_type = "client_credentials"
        });
        client.AccessToken = result.access_token;

        result = client.Post("[IdOfFacebookPage]/feed", new { message = "Test Message from app" });
        //result.id;
        result = client.Get("[IdOfFacebookPage]");

        return false;

Approach to post to a facebook wall:

  1. You need to register in facebook developer tools.
  2. Create a app (fill a form).
  3. Download FGT 5.0.480 (Facebook graph toolkit)
  4. Reference FGT dlls in your web application.
  5. FGT has methods to post to facebook wall but needs appId.
  6. Use the app id of your app created in step 2.

To post to an user's wall through facebook, it is only possible through an app.

Try this asp .net app, which will allow you to post in your wall: https://apps.facebook.com/aspdotnetsample/?fb_source=search&ref=br_tf

This will allow you to envision what you need.

Your app gets an appId with which you need to generate auth token.

Limitation: The user's wall where you want to post should have added the app created at step 2, this is compulsory and there is no work around.

When the user accesses the app for the first time, it automatically asks for permission to post to wall--> the user needs to accept it.

Hope this gives you an idea on how to go about doing this.

You can then post into the user's wall.

For Banshee's below request using Facebook SDK for .NET:

userId - facebook user id, wall to post the message

This user should have added the app created by you in step 1 else it will say unauthorized access.

dynamic messagePost = new ExpandoObject();
messagePost.picture = "http://www.stackoverflow.com/test.png";
messagePost.link = "http://www.stackoverflow.com";
messagePost.name = "This is a test name";

messagePost.caption = "CAPTION 1";
messagePost.description = "Test desc";
messagePost.message = "message"

var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new { 
client_id     = "app_id", 
client_secret = "app_secret", 
grant_type    = "client_credentials" 
});
fb.AccessToken = result.access_token;    

try
{
 var postId = fb.Post(userId + "/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
  //handle oauth exception
}
catch (FacebookApiException ex)
{
  //handle facebook exception
}

You will need an extended token to publish to a wall.. Steps to get extended token is explained well by Banshee..

Edit: How to get extended token by Banshee: Please follow this post

By creating a extended page token and use it to make the post everything works just fine. See this : How to get Page Access Token by code?

Im surprised that this simple task was so hard to get running and that there was vary little help to get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM