问题
I'm tring to get an album picture from facebook using the facebook C# SDK within Silverlight app with the following code:
FacebookClient client = new FacebookClient(this.Profile.AccessToken);
client.GetAsync(string.Format("/{0}/picture?type=small", this.ID));
client.GetCompleted += (s, e) =>
{
dynamic result = e;
};
Where this.ID is the ID of the album, but I get this error: Unexpected character encountered while parsing value: �. Line 1, position 1. from the DeserializeObject method in the JsonSerializer. The problem is that facebook does'n return json data with the imge uri or something like this, but they actually return the image itself in a binary data. Anybody has any idea how I can handle this result or just get Uri to the image? I have a workaround for this using this code:
var request = WebRequest.Create(string.Format("https://graph.facebook.com/{0}/picture?access_token={1}", this.ID, this.Profile.AccessToken));
request.BeginGetResponse(ar =>
{
using (var response = ((WebRequest)ar.AsyncState).EndGetResponse(ar))
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
this.Picture = new BitmapImage(new Uri(response.ResponseUri.AbsoluteUri));
}
);
}
}, request);
But I really wanted to use only Facebook C# SDK for getting the data.
Here is the solution that I'm going to use:
FacebookClient client = new FacebookClient(this.Profile.AccessToken);
client.QueryAsync(String.Format("SELECT src_small, src_big, src FROM photo WHERE pid IN (SELECT cover_pid FROM album WHERE object_id={0})", this.ID));
client.GetCompleted += (s, e) =>
{
dynamic result = e.GetResultData();
Deployment.Current.Dispatcher.BeginInvoke(() => this.Picture = result[0].src_small);
};
回答1:
I was facing a similar issue. Wanted to use Facebook C# SDK only as well. Solved it like this:
FacebookClient facebookAlbumClient = new FacebookClient(_albumAccessToken);
dynamic facebookAlbumCover = facebookAlbumClient.Get(string.Format("/{0}?fields=picture&type=thumbnail", (string)facebookAlbum["id"]));
This way you are getting the json array and not the picture
回答2:
I think I found an acceptable solution for my problem. I'll just use FQL instead of Graph API. This will do the job:
FacebookClient client = new FacebookClient(this.Profile.AccessToken);
client.QueryAsync(String.Format("SELECT src_small, src_big, src FROM photo WHERE pid IN (SELECT cover_pid FROM album WHERE object_id={0})", this.ID));
client.GetCompleted += (s, e) =>
{
dynamic result = e.GetResultData();
Deployment.Current.Dispatcher.BeginInvoke(() => this.Picture = result[0].src_small);
};
来源:https://stackoverflow.com/questions/5706225/facebook-album-picture-using-facebook-c-sharp-sdk