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

时间:2011-12-24 17:03:22

标签: c# windows-phone-7 oauth tweetsharp twitpic

我正在尝试使用WP7应用中的TweetSharp和Hammock库将图片上传到Twitpic。上传照片的代码是:

// 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);

对代码的一些解释:这来自对photoPickerTask的调用,e是包含照片名称和文件(IO.Stream对象)的事件参数。所有这些都经过验证可行。

问题是Twitpic的响应始终是“无法验证您:Twitter拒绝的标题”。 TwitterClient工作,OAuth令牌都可以。 API密钥是正确的。我不知道错误是来自我的代码,来自TweetSharp PrepareEchoRequest()函数还是来自Twitpic。有人能给我一些线索吗?

1 个答案:

答案 0 :(得分:4)

今天我一直有同样的(和类似的)麻烦。我终于通过将版本路径更改为1并将所有令牌输入到请求中来实现它(如twitpic doco中所述)。我以为我昨天尝试了这个确切的组合,但它现在正在运行,所以在此期间手指越过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;
    }