C # and Facebook API

I am currently using the Facebook and C # APIs.

What I'm trying to do is upload an image to an event.

I tried two methods, but none of them work. Can someone please take a look.

Method 1

Dictionary<string, string> args = new Dictionary<string, string>(); string source = "@test.jpg"; string relpath = "/1234456789/photos"; args.Add("message", "sssssss"); args.Add("access_token", api.AccessToken); args.Add("source", source); api.Post(relpath, args); 

Method 2

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("http://graph.facebook.com/1234456789/photos")); request.ContentType = "multipart/form-data"; request.Method = "POST"; string path = HttpUtility.UrlEncode("test.jpg"); 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", path); writer.Write("{0}={1}", "access_token", api.AccessToken); } }, request); 

Method 3

  WebClient client = new WebClient(); byte[] responseBinary = client.UploadFile("http://localhost:61689/Widgets/test2.aspx", "POST", @"C:\test.jpg"); string response = Encoding.UTF8.GetString(responseBinary); Dictionary<string, string> args = new Dictionary<string, string>(); string relpath = "https://graph.facebook.com/me/picture"; args.Add("message", "sssssss"); args.Add("access_token", GetAccessToken(code)); args.Add("source", response); api.Post(relpath, args); 

In method 3, I try to create an answer and write this. I get 400 bad requests.

The image 'test.jpg' is currently located in my website root directory, as well as on its page.

+6
c # facebook
source share
2 answers

Hey Robert, not sure if this is what causes it, but in Example 1 it looks like you are trying to use a string literal for the name JPG, but if so, the @ character should be outside the quotes ...

@ "good morning" // string literal

+1
source share

It looks like you only pass the file name to download. Are you using a library that will read the actual contents of the photo from disk? If not, you will need to do it yourself and write the contents to the request.

The format for writing the contents of the photo into the request will depend on the expectations of the API you are writing (multi-part, etc.).

0
source share

All Articles