1

Looking for some code to upload a photo to facebook using the Graph API in VB.NET. I have the Facebook C# SDK, but it doesn't support uploading photos as far as I can tell.

Accessing the photos works fine and I can send other content to Facebook fine as well. Just not photos.

The facebook documentation talks about attaching the file as a form-multipart request, but I have no idea how to do that. To say that its not very well documented is to put it lightly. Even the guys I hire to do this kind of thing couldn't get it to work.

I've found this: Upload Photo To Album with Facebook's Graph API, but it only describes how to do it in PHP.

I've also seen varying method from different sites about passing the URL of the photo as a part of the HTTP request, but after trying local or remote URLs several times I kept getting a bad url error or something like that.

Any thoughts?

Community
  • 1
  • 1
user548084
  • 489
  • 5
  • 20

1 Answers1

0

You need to pass the Image in a POST request to the Graph API (Need publish_stream permission). What is mentioned in Facebook Documentation is correct. Following is the example code that may do the work. use it inside a method. (Code is in C#)

Legend <content> : you need to provide the info.

Update Please post comments to improve the code.

string ImageData;
string queryString = string.Concat("access_token=", /*<Place your access token here>*/);
string boundary = DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);

StringBuilder sb = String.Empty;
sb.Append("----------").Append(boundary).Append("\r\n");
sb.Append("Content-Disposition: form-data; filename=\"").Append(/*<Enter you image's flename>*/).Append("\"").Append("\r\n");
sb.Append("Content-Type: ").Append(String.Format("Image/{0}"/*<Enter your file type like jpg, bmp, gif, etc>*/)).Append("\r\n").Append("\r\n");

using (FileInfo file = new FileInfo("/*<Enter the full physical path of the Image file>*/"))
{
    ImageData = file.OpenText().ReadToEnd();
}
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());
byte[] fileData = Encoding.UTF8.GetBytes(ImageData);
byte[] boundaryBytes = Encoding.UTF8.GetBytes(String.Concat("\r\n", "----------", boundary, "----------", "\r\n"));
var postdata = new byte[postHeaderBytes.Length + fileData.Length + boundaryBytes.Length];
Buffer.BlockCopy(postHeaderBytes, 0, postData, 0, postHeaderBytes.Length);
Buffer.BlockCopy(fileData, 0, postData, postHeaderBytes.Length, fileData.Length);
Buffer.BlockCopy(boundaryBytes, 0, postData, postHeaderBytes.Length + fileData.Length, boundaryBytes.Length);

var requestUri = new UriBuilder("https://graph.facebook.com/me/photos");
requestUri.Query = queryString;
var request = (HttpWebRequest)HttpWebRequest.Create(requestUri.Uri);
request.Method = "POST";
request.ContentType = String.Concat("multipart/form-data; boundary=", boundary);
request.ContentLength = postData.Length;

using (var dataStream = request.GetRequestStream())
{
      dataStream.Write(postData, 0, postData.Length);
}

request.GetResponse();
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • I finally got around to trying this out but I got the same "The remote server returned an error: (400) Bad Request." that i've gotten with the other methods. I noticed that you declared the request variable twice, vb.net didn't like that. – user548084 Jan 07 '11 at 04:47
  • oh yeah sorry i just edited that.. and for your problem... are you sure u are using a valid access token (with publish stream Extended permission).. because that error normally returned when u dont have a valid access token. Try using that uri with access token in a browser (GET) request do u still get an error... – Shekhar_Pro Jan 07 '11 at 06:43