我正在尝试使用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 token 可以。 API key 正确。我不知道错误是来自我的代码,来自TweetSharp
PrepareEchoRequest()
函数还是来自Twitpic。有人可以给我一个提示吗? 最佳答案
我今天遇到相同(和类似)的问题已经有很多小时了。我终于通过将版本路径更改为1并将所有 token 输入到请求中来使其工作(如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;
}