无法使用Windows Phone上的TweetSharp 2.3.0向媒体发送推文

时间:2013-07-10 18:25:56

标签: c# windows-phone tweetsharp

我遇到了SendTweetWithMedia()的问题。这是我实施的方式

        var service = new TwitterService(TwitterSettings.ConsumerKey, TwitterSettings.ConsumerSecret);
        service.AuthenticateWith(twitterAccess.AccessToken, twitterAccess.AccessTokenSecret);

        string status = string.IsNullOrEmpty(message) ? " " : message;
        var memoryStream = new MemoryStream();
        bitmap.SaveJpeg(memoryStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
        memoryStream.Position = 0;

        var options = new SendTweetWithMediaOptions
        {
            Status = status,
            Images = new Dictionary<string, Stream> {{"picture", memoryStream}}
        };
        service.SendTweetWithMedia(options, (responseStatus, response) => Dispatcher.BeginInvoke(() =>
        {
            memoryStream.Close();
            memoryStream.Dispose();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                UiHelper.ShowToastPrompt(AppResources.TwitterToastPrompt);
            }
            else if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                NavigationService.Navigate(new Uri("/TwitterLoginPage.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show(response.StatusDescription, AppResources.ErrorMessageBoxCaption, MessageBoxButton.OK);
            }
        }));

但它在消息框中显示“禁止”。

即使我尝试从SendTweetWithMediaOptions中删除图片,但它没有帮助。

任何人都可以提供帮助吗?感谢。

1 个答案:

答案 0 :(得分:0)

最后,我可以使用TweetSharp + Hammock使用此代码将照片上传到Twitter

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

将VersionPath的值设置为1非常重要。之前我用过2但是没用。