Thow Thow - 23 days ago 8
C# Question

Visual Studio C# Facebook Graph API can't get user wall

So I'm trying to make an application that gets a user wall from Facebook and puts the posts in a textbox.
I can authenticate and get posts. But when I put the posts in my textbox it puts the first post from the user wall about 30 times in the textbox and nothing else.

This is my code:

WebClient fbAcces = new WebClient();
fbAcces.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
var accessToken = fbAcces.DownloadString("https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=<client_id>&client_secret=<client secret>&fb_exchange_token=<client_accesstoken>");

string token = accessToken.Remove(0, 13);
token = token.Remove(token.Length - 16);

WebClient fbDev = new WebClient();
fbDev.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
string ORPageId = "249269088610051";
var jdata = fbDev.DownloadString("https://graph.facebook.com/" + ORPageId + "/feed?access_token=" + token + "" );
JObject jparse = JObject.Parse(jdata);

var data = jparse["data"];

int i = 0;
foreach(JObject root in data)
{
try
{
facebookTextBox.Text += (string)data[i]["message"] + Environment.NewLine;
facebookTextBox.Text += (string)data[i]["created_time"] + Environment.NewLine;
facebookTextBox.Text += Environment.NewLine;

}
catch (Exception)
{
facebookTextBox.Text = "Error getting user posts";
}
}


In the accestoken variable:
client_id = app id; client_secret = app secret; client_accesstoken = accestoken;

Does anybody know how to fix my problem?
Thanks in advance!

Answer

Try increasing the index

int i = 0;
foreach(JObject root in data)
{
     try
     {
           ...
      }
      catch (Exception)
      {
          ...
      }
      finally
      {
          i++;
      }
}