TwitPic API Isn't Responding Correctly - Keeps Returning 401

348 views Asked by At

So, I've still been trying to make this Twitter client, and I'm working on the image attachments for it, but I can't get the TwitPic API to work with oAuthTwitter (The oAuth framework that I use for the client).

Here's the code:

StringBuilder sb = new StringBuilder();
            filePath = filePath.Replace("\\", "/");
            sb.AppendFormat("?message={1}&media={0}&key={2}&consumer_token={3}&consumer_secret={4}&oauth_token={5}&oauth_secret={6}",
                HttpUtility.UrlEncode("@" + filePath), HttpUtility.UrlEncode(tweet), HttpUtility.UrlEncode(apiKey), HttpUtility.UrlEncode(oAuth.ConsumerKey), HttpUtility.UrlEncode(oAuth.ConsumerSecret), HttpUtility.UrlEncode(oAuth.Token), HttpUtility.UrlEncode(oAuth.TokenSecret));
            string req = sb.ToString();
            debu.Write("[DEBUG]     Request: " + req);
            string response = "hai";
            try
            {
                response = oAuth.WebRequest(oAuthTwitter.Method.POST, "http://api.twitpic.com/1/uploadAndPost.json",
                    req);
            }
            catch (Exception e) { debu.Write("[FATAL]   " + e.Message); return false; }
            debu.Write("[DEBUG]     Response: " + response);

            return true;

The debug file reads the following:

[2/12/2011 3:27:30 PM] [DEBUG]     Request: ?message=This+is+testing+the+image+uploader+for+TwitterAPI&media=C%3a%2fIcon.png&key=33a6804da25ac80ff3d7ba985c6e9a96&consumer_token=EPYl2jwBzUOANlRHC67Q&consumer_secret=yfOz603EbkDat1PBEq05jhvkFuYNvTPwZI1wk2uc&oauth_token=37993700-dU1ecaBlPLBFnVj4LKu6TG5yQRmmlCGr2FX97EqM&oauth_secret=4z6738kJwJaGlJgwiJ5x20prCGccpipcUdnzVpfS6U
[2/12/2011 3:27:31 PM] [FATAL]   The remote server returned an error: (400) Bad Request.

Any suggestions on why this might not be working?

1

There are 1 answers

12
Oded On

You are URL encoding your complete query string.

This means that things like & get encoded as %26 and = as %3D. This is wrong, as your parameters will not be seen as parameters by the API.

You should encode the parameter names and values only.