To do this, first you need to get the application 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);
A few notes about the method above:
- I use the .Net HttpWebRequest instead of the Facebook C # SDK to grab the access_token application because (starting with version 5.011 RC1) the SDK throws a SerializationException. It seems that the SDK is expecting a JSON response from Facebook, but Facebook is returning an access token in the form: access_token = some_value (which is not valid JSON).
HttpHelpers.HttpGetRequest just uses .Net HttpWebRequest. You can also use WebClient, but no matter what you choose, you end up wanting 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 way to get the access_token application, you can generate the application request as follows (here I use the SDK for Facebook C #):
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 get all user application 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; }
Hope this helps.
source share