views:

169

answers:

2

I have recently starting porting a small app of mine to Facebook as a learning experience. I'm quite familiar with Silverlight and .NET in general but haven't done anything on facebook yet. Since all the SDKs and APIs that are available seem to not work, or I wasn't able to use them properly, I decided to directly access Facebook's Graph API and it have been easy so far (I can login, ask for permissions, get profile, albums and photos and post to the users feed). Now I want to upload a photo and this is where I'm really hitting a wall. I use something like this to post a feed:

        WebClient client = new WebClient();
        client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
        client.UploadStringAsync(new Uri(String.Format("https://graph.facebook.com/me/feed")), "POST",
            String.Format("message={0}&link={1}&picture={2}&access_token={3}", "Test", "www.gong.bg", "http://gong.bg/uploads/teams/teams_logos/logo_small_1.png", this.Access_Token));

Quite simple, but it works fine and I don't need more.

For uploading photos I tried to use similar code but with no success, then I decided to try with HttpWebRequest and I now have the following:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("https://graph.facebook.com/me/photos"));
        request.ContentType = "multipart/form-data";
        request.Method = "POST";
        request.BeginGetRequestStream(ar =>
        {
            using (StreamWriter writer = new StreamWriter((ar.AsyncState as HttpWebRequest).EndGetRequestStream(ar)))
            {
                writer.Write("{0}={1}&", "message", HttpUtility.UrlEncode("Test"));
                writer.Write("{0}=@{1}&", "source", HttpUtility.UrlEncode("3.png"));
                writer.Write("{0}={1}&", "access_token", this.Access_Token);
            }
        }, request);

This is not working and I cannot see where the problem is. According to Facebook documentation this should upload a photo to the app default album (create one if doesn't exist)

Thanks all.

A: 

This looks like it might be a simple typo. Try removing that last ampersand so you get this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("https://graph.facebook.com/me/photos"));
request.ContentType = "multipart/form-data";
request.Method = "POST";
request.BeginGetRequestStream(ar =>
{
    using (StreamWriter writer = new StreamWriter((ar.AsyncState as HttpWebRequest).EndGetRequestStream(ar)))
    {
        writer.Write("{0}={1}&", "message", HttpUtility.UrlEncode("Test"));
        writer.Write("{0}=@{1}&", "source", HttpUtility.UrlEncode("3.png"));
        writer.Write("{0}={1}", "access_token", this.Access_Token);
    }
}, request);
Steve Danner
Yes, this was really a typo, but fixing it didn't solve the problem.
Petar Slavov
A: 

Try my Facebook .Net SDK on codeplex. The most recent source supports silverlight. http://facebooksdk.codeplex.com

You can do what you are attempting like this:

        byte[] photo = File.ReadAllBytes(photoPath);
        FacebookApp app = new FacebookApp();
        dynamic parameters = new ExpandoObject();
        parameters.access_token = "access_token";
        parameters.caption = "Test Photo";
        parameters.method = "facebook.photos.upload";
        parameters.uid = ConfigurationManager.AppSettings["UserId"];
        var mediaObject = new FacebookMediaObject
        {
            FileName = "monkey.jpg",
            ContentType = "image/jpeg",
        };
        mediaObject.SetValue(photo);
        parameters.source = mediaObject;
        app.ApiAsync((ar, state) => { 
            var postId = (string)ar.Result;
        }, null, parameters, HttpMethod.Post);
Nathan Totten