facebook c# sdk getting started

放肆的年华 提交于 2019-12-17 22:37:40

问题


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


回答1:


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!




回答2:


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



来源:https://stackoverflow.com/questions/8720023/facebook-c-sharp-sdk-getting-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!