Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Facebook Requests 2.0 with the C# SDK

I am trying to update the bookmark count field with the SDK but have not had any success yet.

Can somebody tell me what classes I need to instantiate to do something similar to the following link:

http://developers.facebook.com/blog/post/464

Note:

The link demonstrates how to set the bookmark count and delete it. I would like to be able to do the same with the SDK, any help would be appreciated.

like image 230
PazoozaTest Pazman Avatar asked May 09 '11 02:05

PazoozaTest Pazman


1 Answers

To do this, first you need to get you app's access token:

    private string GetAppAccessToken() {

        var fbSettings = FacebookWebContext.Current.Settings;

        var accessTokenUrl = String.Format("{0}oauth/access_token?client_id={1}&client_secret={2}&grant_type=client_credentials",
            "https://graph.facebook.com/", fbSettings.AppId, fbSettings.AppSecret);

        // the response is in the form: access_token=foo
        var accessTokenKeyValue = HttpHelpers.HttpGetRequest(accessTokenUrl);
        return accessTokenKeyValue.Split('=')[1];
    }

A couple of things to note about the method above:

  • I'm using the .Net HttpWebRequest instead of the Facebook C# SDK to grab the app access_token because (as of version 5.011 RC1) the SDK throws a SerializationException. It seems that the SDK is expecting a JSON response from Facebook, but Facebook returns the access token in the form: access_token=some_value (which is not valid JSON).
  • HttpHelpers.HttpGetRequest simply uses .Net's HttpWebRequest. You can just as well use WebClient, but whatever you choose, you ultimately want to make this http request:

    GET https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials HTTP/1.1 Host: graph.facebook.com

Now that you have a method to retrieve the app access_token, you can generate an app request as follows (here I use the Facebook C# SDK):

public string GenerateAppRequest(string fbUserId) {
    var appAccessToken = GetAppAccessToken();
    var client = new FacebookClient(appAccessToken);
    dynamic parameters = new ExpandoObject();
    parameters.message = "Test: Action is required";
    parameters.data = "Custom Data Here";

    string id = client.Post(String.Format("{0}/apprequests", fbUserId), parameters);
    return id;
}

Similarly, you can retrieve all of a user's app requests as follows: Note: you probably don't want to return "dynamic", but I used it here for simplicity.

   public dynamic GetAppRequests(string fbUserId) {
        var appAccessToken = GetAppAccessToken();
        var client = new FacebookClient(appAccessToken);

        dynamic result = client.Get(String.Format("{0}/apprequests", fbUserId));
        return result;    
    }

I hope this helps.

like image 89
Johnny Oshika Avatar answered Nov 03 '22 02:11

Johnny Oshika