8

I would like to write a console application that automatically posts information to my wall once every morning.

I have signed up for facebook developer and have a AppID and App Secret

I have been trying to play with the C# facebook SDK and looked through several examples.

Seems like the examples get a user token - but have to use a browser that is in windows forms. This is an automated process - so I dont want to have a user present.

Ive also created some examples using the application token - but it does not seem to be able to write to the wall.

I wrote the Twitter equivalent very quickly. I must be missing something here ???

What is the proper way to proceed?

It seems that all I should need to is: FaceBookClient(appID, appSecret) and then just FaceBookClient.Put(message) ???

clarification added:

Playing with the C# facebook sdk winform application I had to change their FacebookLoginDialog.cs to use the folling URL:

https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=APPID&client_secret=APPSECRET&scope=user_about_me,publish_stream,offline_access

which returns an accesskey in WebBrowser.DocumentText

If I then call:

            var fb = new FacebookClient(_accessToken);
            dynamic parameters = new ExpandoObject();
            parameters.message = "Hello World!";
            dynamic result = fb.Post("me/feed", parameters);

I get the exception:

(OAuthException) An active access token must be used to query information about the current user.

If I change the code above to NOT use that access token - but use the appID and Appsecret:

           FacebookClient myFacebookClient = new FacebookClient("APPID", "APPSECRET");
            dynamic parameters = new ExpandoObject();
            parameters.message = "Hello World!";
            dynamic result = myFacebookClient.Post("me/feed", parameters);

Then I get the exception:

(OAuthException) An active access token must be used to query information about the current user.

I guess it is the same exception

Kiquenet
  • 13,271
  • 31
  • 133
  • 232
Bubba
  • 182
  • 1
  • 2
  • 13

2 Answers2

3

Here is what I found.

Download the facebook C# sdk source code and samples from http://facebooksdk.codeplex.com/

Unzip the code and load into Visual Studio the Facebook C# SDK sample called CS-WinForms.

At the top of Form1.cs - enter your application ID

Run the application.

Form1.cs pops up with a button "Login to Facebook". Click the button.

FacebookLoginDialog.cs pops up with a browser window in it displaying facebook asking for permissions.

FacebookLoginDialog.cs creates a browser window that will go to your user on facebook and request permissions. By default those permissions are: user_about_me,publish_stream,offline_access.

Offline_access means the AccessToken you get - never expires

Click "OK" in Facebook to allow the application to access your facebook data.

FacebookLoginDialog.cs should find that you logged in and get the access token that never expires.

The access token is a string.

Insert a break point so that you can copy this access token. Save this access token as you can use it from now on to access to Facebook.

Facebook developers website has some tools that you can use to check the access token https://developers.facebook.com/tools/debug/access_token You can enter your access token and click "Debug" and it should list your applicationID, UserID, and for "expires" it should say "never".

Once you have this access token - then you can simply write code like:

                        var fb = new FacebookClient(AccessToken);
                        dynamic parameters = new ExpandoObject();
                        parameters.message = FacebookString;
                        dynamic result = fb.Post("me/feed", parameters);
                        var id = result.id;

to post message to Facebook!

Bubba
  • 182
  • 1
  • 2
  • 13
  • 2
    Hello i cannot find the sample you are talking about can you send me a link of it ? – Z. Kiwan Dec 30 '16 at 09:20
  • 1
    Howdy, its been a long time since the original post does anyone have a working link to any sample code. The Wayback Machine doesn't have any records. – samtrod Jul 21 '18 at 20:45
0

You should request offline_access in addition to publish_stream. Once you have the never-expiring token, store that in your app.

DMCS
  • 31,463
  • 14
  • 68
  • 103
  • From the facebook sdk winform sample application - I had aboutme, publishstream, and offlineaccess. – Bubba Jan 04 '12 at 01:17
  • Run the user access token you get back thru: https://developers.facebook.com/tools/debug to check it out. – DMCS Jan 04 '12 at 03:10