简体   繁体   English

使用TweetSharp / Hammock将媒体上传到Twitpic时,“Twitter拒绝了”

[英]“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. 我正在尝试使用WP7应用程序中的TweetSharp和Hammock库将图片上传到Twitpic。 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). 对代码的一些解释:这来自对photoPickerTask的调用, e是包含照片名称和文件(IO.Stream对象)的事件参数。 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". 问题是Twitpic的响应始终是“无法验证您:Twitter拒绝的标题”。 The TwitterClient works, the OAuth tokens are all right. TwitterClient工作,OAuth令牌都可以。 The API Key is correct. API密钥是正确的。 I don't know if the error comes from my code, from the TweetSharp PrepareEchoRequest() function or from Twitpic. 我不知道错误是来自我的代码,来自TweetSharp PrepareEchoRequest()函数还是来自Twitpic。 Can anybody give me a clue? 谁能给我一个线索?

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). 我终于通过将版本路径更改为1并将所有令牌输入到请求中来实现它(如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. 我以为我昨天尝试了这个确切的组合,但它现在正在工作,所以手指越过api在此期间没有更新。

    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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM