6

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.

1 Answer 1

6

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.

3
  • access to /apprequests assumes the user has authenticated/installed your application, correct? Commented Sep 6, 2011 at 16:13
  • I don't believe the user has to authenticate your app. According to Facebook's documentation, you only need your app access token: developers.facebook.com/docs/reference/api/user Commented Sep 7, 2011 at 13:47
  • Nope. Tested with my app access token and still get "All users in param ids must have accepted TOS" Commented Sep 7, 2011 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.