Question

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?

Was it helpful?

Solution

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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top