将图像直接上传到twitter

时间:2012-05-03 11:11:25

标签: c# windows-phone-7 twitter

我需要帮助将图像直接上传到Windows Phone 7中的Twitter。

我完成了Twitter的oauth流程,也可以更新推文,但是我无法使用wp7将图片上传到Twitter?

2 个答案:

答案 0 :(得分:9)

我通过使用Hammock.WindowsPhone.Mango库为此制定了解决方案。 (TweetSharp内部使用Hammock库来实现oAuth和其他功能,但我从未使用过TweetSharp或Twitterizer)

我已经从Nuget

安装了最新版本的Hammock

然后使用以下代码将照片上传到Twitter:

public void uploadPhoto(Stream photoStream, string photoName)
{
var credentials = new OAuthCredentials
        {
            Type = OAuthType.ProtectedResource,
            SignatureMethod = OAuthSignatureMethod.HmacSha1,
            ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
            ConsumerKey = TwitterSettings.consumerKey,
            ConsumerSecret = TwitterSettings.consumerKeySecret,
            Token = TwitterSettings.accessToken,
            TokenSecret = TwitterSettings.accessTokenSecret,
            Version = "1.0a"
        };


        RestClient restClient = new RestClient
        {
            Authority = "https://upload.twitter.com",
            HasElevatedPermissions = true,
            Credentials = credentials,
            Method = WebMethod.Post
         };
         RestRequest restRequest = new RestRequest
         {
            Path = "1/statuses/update_with_media.json"
         };

         restRequest.AddParameter("status", tbxNewTweet.Text);
         restRequest.AddFile("media[]", photoName, photoStream, "image/jpg");

}

    restClient.BeginRequest(restRequest, new RestCallback(PostTweetRequestCallback));
}


private void PostTweetRequestCallback(RestRequest request, Hammock.RestResponse response, object obj)
{
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
        //Success code
        }
}

下面, photoName是所选图像的名称(“e.OriginalFileName”) photoStream是PhotoChooserTask的“e.ChosenPhoto”

并且.AddFile()的第四个参数应该小心(我在做这个示例时没有考虑其他格式,你必须小心你的应用程序)

我希望这会有所帮助!!

答案 1 :(得分:0)

LINQ to Twitter支持WP7,并且有一个TweetWithMedia方法,其工作原理如下:

    private void PostButton_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(TweetTextBox.Text))
            MessageBox.Show("Please enter text to tweet.");

        ITwitterAuthorizer auth = SharedState.Authorizer;
        if (auth == null || !auth.IsAuthorized)
        {
            NavigationService.Navigate(new Uri("/OAuth.xaml", UriKind.Relative));
        }
        else
        {
            var twitterCtx = new TwitterContext(auth);

            var media = GetMedia();

            twitterCtx.TweetWithMedia(
                TweetTextBox.Text, false, StatusExtensions.NoCoordinate, StatusExtensions.NoCoordinate, null, false,
                media,
                updateResp => Dispatcher.BeginInvoke(() =>
                {
                    HandleResponse(updateResp);
                }));
        }
    }