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

发布于 2024-12-22 16:08:35 字数 868 浏览 2 评论 0原文

我正在尝试使用 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 .流对象)。所有这些都已验证有效。

问题是 Twitpic 的响应始终是“无法对您进行身份验证:标头被 Twitter 拒绝”。 TwitterClient 可以工作,OAuth 令牌也正常。 API 密钥正确。我不知道错误是否来自我的代码、TweetSharp PrepareEchoRequest() 函数或 Twitpic。有人可以给我线索吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-12-29 16:08:35

今天我已经有太多时间遇到同样(和类似)的麻烦了。我最终通过将版本路径更改为 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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文