Skip to content

Instantly share code, notes, and snippets.

@anuith
Created June 24, 2012 20:00
Show Gist options
  • Save anuith/2984688 to your computer and use it in GitHub Desktop.
Save anuith/2984688 to your computer and use it in GitHub Desktop.
Windows Phone Hackathon : Facebook & Instagram Sample - Facebook Page Code - Get Feed and ReadFeed()
private void ButtonGo_Click(object sender, RoutedEventArgs e) {
string facebookname = TextBox_FBName.Text;
// Reset textblocks, image, and listbox
Image_Cover.Source = null;
TextBlock_Desc.Text = "";
TextBlock_Name.Text = "";
ListBox_Photos.Items.Clear();
// Initializes new Facebook Client
FacebookClient client = new FacebookClient(accessToken);
client.GetCompleted += new EventHandler<FacebookApiEventArgs>(client_GetCompleted);
// Get profile info
client.GetAsync(facebookname, null, "profile");
// Get feed
client.GetAsync(facebookname + "/feed", null, "posts");
// Get profile picture
Image_Profile.Source = new BitmapImage(
new Uri("https://graph.facebook.com/" + facebookname + "/picture"));
}
private void client_GetCompleted(object sender, FacebookApiEventArgs e) {
JObject json = JObject.Parse(e.GetResultData().ToString());
string userstate = (string)e.UserState;
if ("profile" == userstate) {
Deployment.Current.Dispatcher.BeginInvoke(() => SetPageInfo(json));
}
else if ("posts" == userstate) {
Deployment.Current.Dispatcher.BeginInvoke(() => ReadFeed(json));
}
}
private void ReadFeed(JObject values)
{
List<FacebookItem> items = new List<FacebookItem>();
JToken data = values["data"];
foreach (JToken token in data) { // Get data from JSON to object one-by-one
FacebookItem item = new FacebookItem();
item.id = token["id"].Value<string>();
if (token["message"] != null) {
item.message = token["message"].Value<string>();
}
else if (token["story"] != null) {
item.message = token["story"].Value<string>();
}
if (token["picture"] != null) {
if (token["type"].Value<string>() == "photo") {
string object_id = token["object_id"].Value<string>();
item.picture = string.Format(
"http://graph.facebook.com/{0}/picture?type=normal",
object_id);
} else {
item.picture = token["picture"].Value<string>();
}
}
if (token["link"] != null) {
item.link = token["link"].Value<string>();
}
if (token["likes"] != null) {
item.likesCount = token.SelectToken("likes.count").Value<int>();
}
if (token["comments"] != null) {
item.commentsCount = token.SelectToken("comments.count").Value<int>();
}
item.created_time = token["created_time"].ToObject<DateTime>();
item.updated_time = token["updated_time"].ToObject<DateTime>();
items.Add(item);
}
ListBox_Photos.ItemsSource = items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment