Using Facebook 2.0 Queries Using the C # SDK

I am trying to update the bookmark counting field using the SDK, but have not yet succeeded.

Can someone tell me which classes I need to create in order to do something similar to the following link:

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

Note:

The link shows how to set the number of bookmarks and delete it. I would like to be able to do the same with the SDK, any help would be appreciated.

+6
source share
1 answer

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); // the response is in the form: access_token=foo var accessTokenKeyValue = HttpHelpers.HttpGetRequest(accessTokenUrl); return accessTokenKeyValue.Split('=')[1]; } 

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.

+6
source

Source: https://habr.com/ru/post/887586/


All Articles