Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Headers rejected by Twitter" when uploading media to Twitpic with TweetSharp/Hammock

I'm trying to upload a picture to Twitpic using TweetSharp and Hammock libraries in a WP7 app. The piece of code which uploads the photo is this:

// Clients.srv is a TweetSharp TwitterClient
RestRequest req = Clients.srv.PrepareEchoRequest();
RestClient client = new RestClient { Authority = "http://api.twitpic.com/", VersionPath = "2" };

req.AddFile("media", e.OriginalFileName, e.ChosenPhoto);
req.AddField("key", "hidden");
req.AddField("message", Tweet.Text);
req.Path = "upload.xml";
req.Method = Hammock.Web.WebMethod.Post; 

client.BeginRequest(req, (RestCallback) uploadCompleted);

Some explanation to the code: this comes from a call to photoPickerTask, e is the event argument which contains the photo name and file (an IO.Stream object). All of this is verified to be working.

The problem is that the response of Twitpic is always "Could not authenticate you: headers rejected by Twitter". The TwitterClient works, the OAuth tokens are all right. The API Key is correct. I don't know if the error comes from my code, from the TweetSharp PrepareEchoRequest() function or from Twitpic. Can anybody give me a clue?

like image 857
gjulianm Avatar asked Oct 09 '22 04:10

gjulianm


1 Answers

I've been having the same (& similar) trouble for too many hours today. I finally got it to work by changing the version path to 1 and entering all tokens into the request (as described in the twitpic doco). I thought I tried this exact combination yesterday, but it is working now, so fingers crossed the api isn't updated in the meantime.

    TwitterService service = new TwitterService(consumerKey, consumerSecret);
    service.AuthenticateWith(accessToken, accessTokenSecret);

    if (thumbnail != null)  // an image post - go through twitpic
    {
        MemoryStream ms = new MemoryStream();
        thumbnail.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        ms.Seek(0, SeekOrigin.Begin);

        // Prepare an OAuth Echo request to TwitPic
        RestRequest request = service.PrepareEchoRequest();
        request.Path = "uploadAndPost.xml";
        request.AddField("key", twitpicApiKey);
        request.AddField("consumer_token", consumerKey);
        request.AddField("consumer_secret", consumerSecret);
        request.AddField("oauth_token", accessToken);
        request.AddField("oauth_secret", accessTokenSecret);
        request.AddField("message", "Failwhale!");
        request.AddFile("media", "failwhale" + Environment.TickCount.ToString(), ms, "image/jpeg");

        // Post photo to TwitPic with Hammock
        RestClient client = new RestClient { Authority = "http://api.twitpic.com/", VersionPath = "1" };
        RestResponse response = client.Request(request);

        return response.Content;
    }
like image 104
James Brown Avatar answered Oct 13 '22 11:10

James Brown