1

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;
Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Those links use different libraries. Which one are you using?. Facebook C# SDK (http://facebooksdk.net/) will give let you do what you need. – Daniel May 07 '14 at 18:24
  • Im not using any library right now, but Im looking for the easiest and best way to do this. I will look in to facebooksdk.net. If you have a guide on how to do what I need it would be even better. – Banshee May 08 '14 at 09:33
  • IMO Facebook C# SDK is the way to go, it is easy to use and gives you the API you need. This page gives you examples of how to get/post data to Facebook: http://facebooksdk.net/docs/making-synchronous-requests/ – Daniel May 08 '14 at 12:42
  • http://dotnetdevtricks.blogspot.in/2012/09/facebook-c-sdk-for-aspnet.html ? – Vrashabh Irde May 12 '14 at 12:45
  • @Slartibartfast I need to do it all from within my website, no login forms and so on. When users post messages on my website I need to post them on my facebook page and the user should not have to do anything to get this going. – Banshee May 13 '14 at 11:21
  • @Daniel The problem is to give my created facebook app rights to post on my facebook page. What I need to do is when users post messages on my website I want the ASP.NET website to post the same message on ti my websites facebook page with the created facebook app. But when I try to post I get access failed. – Banshee May 13 '14 at 11:24
  • You are the Page Admin... so YOU (not the user) must generate the Long Live Access Token, store it somewhere, and use it to post those "comments" to your page. You can generate that token by creating an admin page (just for you) that does the login and generate the token (do this ONCE, just to get to token)... once you have that token, it WILL NOT expire until you manually revoke access, your password changes or Facebook invalidates it (it has happened to me!)... then when the USER post a comment to your site, your server posts the comment to FB using YOUR previously created token. – Daniel May 13 '14 at 14:40
  • You need the permission "manage_pages" (https://developers.facebook.com/docs/facebook-login/permissions/v2.0) which will let you create Long Live Access Tokens that do not expire. Go to https://developers.facebook.com/docs/facebook-login/access-tokens and look for "Extending Page Access Tokens" – Daniel May 13 '14 at 14:42
  • @Daniel, yes I have been there but how do I do this with the Facebook SDK .NET? All examples I see is to manually work with http request/response? – Banshee May 13 '14 at 18:14

2 Answers2

3

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

Community
  • 1
  • 1
  • Why not use Facebook SDK .NET? I have already implemented some of this Facebook SDK .NET code but the problem is that I got no access to post on to the websites facebook page? – Banshee May 13 '14 at 11:26
  • Hey Banshee, I have edited the answer below. Please try and let me know if you were able to achieve this. – Adithya Kumaranchath May 13 '14 at 12:15
  • Thanks, but no, the same exception : Additional information: (OAuthException - #200) (#200) The user hasn't authorized the application to perform this action. Im sure that I run the same code as you suggest. Have you tested the code? If so, then maybe I got some problems with settings in the facebook app? All examples I seen are involving a webpage where the user have to autorize manually and this is not what I am looking for. I need my webpage to post as my facebook app to the facebook page. The facebook app should be authorized to post(i hope). – Banshee May 13 '14 at 18:11
  • Did you add the app to the user ? also in the app settings you need to add publish_stream and publish_actions permissions. – Adithya Kumaranchath May 13 '14 at 18:22
  • If you are looking to paste in a facebook page, just specify the pageId instead of userId. But the admin of the page should have given permission to publish in the wall. – Adithya Kumaranchath May 13 '14 at 18:25
  • You can also post to a facebook page as a user if you are the admin of that page or have publish_stream privileges. Try this.. go to the below url generate an access token.. Use this access token and post using your code and test it. https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Did%2Cname&version=v2.0 – Adithya Kumaranchath May 13 '14 at 18:36
  • Go to stated webpage, click get access token, mark all UserData and Extended Permissions, Get Access token, copy token to the FaceBookClient constructor, run, get error : "(OAuthException - #200) (#200) Insufficient permission to post to target on behalf of the viewer" – Banshee May 13 '14 at 21:20
  • I have added the exact code that I have right now in first post(Edit). My user is admin of the Facebook page but I really want to post to the wall as the website itself(I supose this will be the Facebook App). – Banshee May 13 '14 at 21:24
  • You talk about publish_stream and publis_action, where do I find this? I have just created the facebook app and done nothing more with it. – Banshee May 13 '14 at 21:31
  • Check this out.. Do you have all these permissions .. http://postimg.org/image/8xbmn7zlb/ – Adithya Kumaranchath May 15 '14 at 08:59
  • No, it states that it is not available to all users becouse your app is not live? – Banshee May 15 '14 at 11:06
  • I have no removed my app and created a new one. I have also set a contact email so the app could go live so now the permissions should be correct. But does not the app need to have any knolege about the facebook website Im about to work(post/read) agains? – Banshee May 15 '14 at 11:12
  • This page was recently modified. Previously, it used to have permission list for the app including publish_stream rights. Now they have deprecated publish_stream and made it simple. I think you need request for these permissions by clicking the submit button. The app need not have knowledge about the page, but your facebook page must let other users publish content on the page wall. – Adithya Kumaranchath May 15 '14 at 11:16
  • Can you explain in greater detail what I need to do? Do I need to Add platform under Settings > Basic? And if so, what? There is a Submit itmes for Approval under Status & Review, do I need to add anything here? And so, what? – Banshee May 15 '14 at 16:49
  • If I try the facebook dev explorer(https://developers.facebook.com/tools/explorer/) and typ [facebook page id]/feed?name=hello&message=hello_from_url and run a post I will get the same error("(#200) The user hasn't authorized the application to perform this action", "OAuthException", 200)? – Banshee May 15 '14 at 16:56
  • can you share the solution with me ? I can get it running for you – Adithya Kumaranchath May 16 '14 at 05:39
  • Great, here is a link to a project I have : http://bradspel.net/test/FacebookTest.zip I do however believe that the problem is within the Facebook app. Im not sure how I could share it. I could set you as administrator but then I need a account to invite. Just run the website and you will get an exception when it tries to post the message to the facebook page. – Banshee May 16 '14 at 19:23
  • Thanks for your hard work! But I need some more help here : 1. Im new to landing pages but when googling it states that the Landing page is replaced with Facebook App? When in Settings > add plaform > facebook app there is nothing called landingPage? There is Canvas Page, Canvas URL and Secure Canvas URL? 2. Do I need to create this landing page as a regular html page in my website? 3. Status & Review > Start a Submissions(only publish_actions) I get warning about that I need to add 4 pictures that shows how I use facebook in my app? What does this really mean for me? – Banshee May 19 '14 at 10:23
  • Yes. Landing page was just a term used by me to denote Canvas URL. You need to create a regular aspx page and you would need to write some code that would invoke the oauth dialog. And regarding Status & Review, the 4 pictures are for you to give a reason to the moderators to approve your publish_actions privilege. This was a new addition recently, I guess it for them to understand if the app is genuine or not. Hey !! and no problem man.. glad to help. I want you to get your app up and running.. !! Cheers... – Adithya Kumaranchath May 19 '14 at 12:24
  • Okay? So I need to create a webpage at my website that works as Canvas URL and this webpage needs to handle the permissions? I dont really get it, all I want is for my site to post a couple of messages to my wall? This will not be triggered by a user so all permissions could be set direcly from code. I thought the app id and secreate question would give the permissions but no? I need another webpage and my site that is handling what really? I havent seen this part in the examples I followed so far. If the users was to autenticate it self to post I would understand the extra aspx page. – Banshee May 19 '14 at 17:11
  • Yes. The webpage will be the landing page for the facebook app. i.e when ever any user searches for this app via facebook and loads it.. the aspx page will be loaded.. and when it is loaded it will show a dialog that will ask the user if the app can publish anything in user's wall.. – Adithya Kumaranchath May 21 '14 at 08:19
  • Okay? But I dont need that functionallity? I only need my app to post on a wall? This seems vary complicated? I have never walked away from a task like this but this is a bit to much. Do you maybe got a full project that contains all the pices that are needed to do a facbook post? – Banshee May 21 '14 at 17:17
  • 1
    Shouldn't be very complicated Banshee. I feel that you are on the right track it is just that you need to understand clearly how facebook oauth works.. Let me see if I can put together something because am on travel.. – Adithya Kumaranchath May 22 '14 at 12:07
  • Hi, have you found a solution? I dont really know what more to do? – Banshee Jun 02 '14 at 08:20
  • Sorry Banshee.. I am on travel.. I am currently unable to put something together for you.. – Adithya Kumaranchath Jun 04 '14 at 13:15
  • If you got the time, pleas help me with a sample. – Banshee Jul 04 '14 at 09:38
  • Any time for this yet? Would be vary glad for help. – Banshee Aug 09 '14 at 22:53
1

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.

Community
  • 1
  • 1
Banshee
  • 15,376
  • 38
  • 128
  • 219