Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# & Facebook API

Tags:

c#

facebook

I'm currently using the Facebook API and C#.

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

I've tried two methods, but neither seem to work. Could 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'm trying to create the response and write that. I'm getting 400 bad request.

The image 'test.jpg' currently sits in my website root, same as the page calling it.

like image 434
Robert Avatar asked Jan 24 '11 16:01

Robert


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


1 Answers

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

@"good morning" // a string literal

like image 80
Jesse McCulloch Avatar answered Oct 06 '22 01:10

Jesse McCulloch