Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook integration with C# desktop application

im just going to dive straight in and give you a little background on what im trying to do, what i've tried, and the obstacles in my way. so here goes..

MY GOAL
To post to a facebook profile or wall from a desktop application.
The desktop application will be used by individual users who have their own facebook account.
The aim is to create each user their own facebook app and get the app id and app secret.
And then use the app id and app secret in the desktop application ( Saved somewhere in database or config ) to allow the user of the desktop application to post to their profile without having to enter their email address and password to login first

So in summary, 1 facebook app per facebook account.
The app settings will be saved in each users desktop application Post to their own facebook via their own facebook app without logging in

The main emphasis here being that the user does not have to log in manually via a window or browser.

I have created my facebook app and have set it to live status.. i have also added in all possible permissions and extended permissions in the settings ( Within facebook ) just to make sure i wasnt missing anything. So I do have my AppID and App secret.. I also have my Client Token which it says to use in place of the app secret for 'auth methods'. I use this to get my access token. I tried the app secret and it doesnt return the access token

MY ATTEMPTS :

C# FACEBOOK SDK So, i started with and am still trying to use the c# sdk
I can retrieve my access token but cannot post.

I get he below errors all the time with whatever i try... these are just 2 of the many code examples i have tried.
(OAuthException - #2500) An active access token must be used to query information about the current user.

dynamic r = fb.Post("me/feed", new { message = "My seconds wall post using Facebook C# SDK" });

(OAuthException - #190) The client token cannot be used for this API

dynamic r = fb.Post("kevin.maguire.965/feed", new { message = "My second wall post using Facebook C# SDK" });

I read the following extract from the below link which states my access token is an app token and i need a access token for a user or page? Need Help on OAuthException Code 2500 Error 2500 means you have no access token or an app access token but are trying to access /me/ - 'me' is a placeholder for 'current user or page ID' so won't be valid without an access token for a user or page

So, i have tried to get the userID back using the following Answer ( Facebook C# SDK Get Current User )

var fb = new FacebookClient("access_token");
dynamic result = fb.Get("me", new [] { fields = "id" });
var userId = result.id;

I get the access token which i assume is the app token and not the user token

dynamic result = fb.Get("oauth/access_token", new
        {
            client_id = this.ApplicationId,
            client_secret = this.AppSecret,
            grant_type = "client_credentials"
        });

fb.AccessToken = result.access_token;

So i have no idea at this moment in time how to post to my profile

I am able to achieve the above using twitter where i can use the secret, token, id etc ... they provide and I can successfully post to my twitter account from a desktop application WITHOUT logging into my twitter account.

Another user has also found it quite easy to post to twitter without any real issues. ( facebook c# sdk getting started )

He also seems to have had success which i have not using the same code - this code was uses in June 2012 so there could have been breaking changes released since then.
I got the message : (OAuthException - #2500) An active access token must be used to query information about the current user... when i used the sdk. When i tried to get the access token using a web request and then pass that token to the sdk object to create a facebookclient i got this message (OAuthException - #190) Invalid OAuth access token signature.

    WebRequest request = WebRequest.Create("https://graph.facebook.com/oauth/access_token?      grant_type=client_credentials&client_id=999999999999999&client_secret=edefefr8e09r8e08r080r8er0e");

        request.Method = "POST";

        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";

        // Get the request stream.
        Stream dataStream = request.GetRequestStream();

        // Close the Stream object.
        dataStream.Close();

        // Get the response.
        WebResponse response = request.GetResponse();
        dataStream = response.GetResponseStream();

        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);

        // Read the content.
        string accesstoken = reader.ReadToEnd();
        MessageBox.Show("accesstoken") 

        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

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

Obviously in the code above i changed the id and secret to dummy values.

So basically folks...the above links and above code are only a pinch of what I have tried to date in the last few days... i've basically ran out of options and am missing something here which could be averting my attention easily.. or maybe not :) i dont know.

If any one would even have a simple windows form, or wpf window application example using the c# sdk or using restsharp or just using WebRequest object with 'POST' method then I would be eternally greatful.

Just to iterate again that it is a desktop application and not ASP.net .

Many thanks folks for your attention and time.

enter image description here

enter image description here

enter image description here

enter image description here

like image 245
Kev Avatar asked May 08 '13 14:05

Kev


1 Answers

From what I can see you are supplying an incorrect access token. Since you haven't provided the code with regards to obtaining the access token, may I suggest you take a look at this link which explains how to build a Facebook application, including obtaining an access token via a WebBrowser control.

EDIT: You are supplying the app access token but are trying to post as a user. For this operation you need a user access token which you can obtain by following the steps in the link above.

like image 151
Jurgen Camilleri Avatar answered Nov 15 '22 08:11

Jurgen Camilleri