5

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

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

4

2 回答 2

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() 的第四个参数(我在做这个示例时没有考虑其他格式,你必须在你的应用程序中小心)

我希望这有帮助!!

于 2012-06-06T11:09:59.027 回答
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);
                }));
        }
    }

于 2012-06-13T22:50:15.537 回答