Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

facebook c# sdk getting started

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...