Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post comment to facebook wall using asp.net

I have a website that I have registered as a facebook app - I now have an app ID.

My website is ASP.net C#. When the user clicks a button I'd like it to post a pre-defined message to their wall. I'm expecting Facebook to present a login dialog to the user - they login and grant publish permission to for my website app.

Does anyone have any sample code that would do this? I think I need to use the graph API but all the examples I've seen use PHP - which I know nothing about. I'm looking for an example that would use Java Script (of which I know almost nothing) or C# (beautiful!).

* Update *

I have managed to get the access_token. Now I make a call through the Facebook C# API to post to the wall. I get the error message:

(#803) Some of the aliases you requested do not exist: profile_id

I've stepped through the api code and found that it is trying to post to the following address: {https://graph.facebook.com/PROFILE_ID/feed}, the post data is: message=Sample+message+from+c%23+sdk&access_token=199209316768200|2.1avFTZuDGR4HJ7jPFeaO3Q__.3600.1302897600.1-100000242760733|R4DkNDf4JCb6B2F64n5TSQwBqvM

I'm pretty sure my token should be valid. Prior to requesting access token I requested publish_stream on the app authorization request as follows:

Response.Redirect ("https://www.facebook.com/dialog/oauth?client_id=" + myAppId + "&redirect_uri=" + myURL + "&scope=publish_stream");

The sdk code that actually makes the request is as follows:

private string MakeRequest(Uri url, HttpVerb httpVerb,
                                   Dictionary<string, string> args)
        { 
        if (args != null && args.Keys.Count > 0 && httpVerb == HttpVerb.GET)
        {
            url = new Uri(url.ToString() + EncodeDictionary(args, true));
        }

        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

        request.Method = httpVerb.ToString();

        if (httpVerb == HttpVerb.POST)
        {
            string postData = EncodeDictionary(args, false);

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] postDataBytes = encoding.GetBytes(postData);

            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDataBytes.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(postDataBytes, 0, postDataBytes.Length);
            requestStream.Close();
        }

        try
        {
            using (HttpWebResponse response 
                    = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader 
                    = new StreamReader(response.GetResponseStream());

                return reader.ReadToEnd();
            }
        }

Can anyone see what I'm doing wrong?

Many thanks,

Rob.

like image 336
Rob Bowman Avatar asked Oct 12 '22 08:10

Rob Bowman


1 Answers

First of all, you need to take care of Authentication. You need to create an Application, and use OAuth to get hold of the access token. It's all described in the Authentication guide.

To post something to the user's wall, take a look at the Graph API under Publishing.

As a start, you could use Facebook's C# SDK

like image 115
MartinHN Avatar answered Oct 21 '22 05:10

MartinHN