Upload image directly to twitter

2019-04-09 07:43发布

I need help in uploading images directly to twitter in Windows Phone 7.

I am done with oauth flow of twitter and can also could update tweets but I have not been able to upload image to twitter using wp7?

2条回答
【Aperson】
2楼-- · 2019-04-09 08:06

I have worked out a solution for this, by using the Hammock.WindowsPhone.Mango library. (TweetSharp internally uses Hammock library for oAuth and other functionalities, but I have never used TweetSharp or Twitterizer)

I have installed the latest version of Hammock from Nuget

And then the following code is used for photo upload to 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
        }
}

Here, photoName is the name of the image selected ( "e.OriginalFileName") photoStream is the "e.ChosenPhoto" from the PhotoChooserTask

and the 4th parameter for .AddFile() should be taken care (I have not considered other formats while doing this sample, you have to take care in your apps)

I hope this helps!!

查看更多
萌系小妹纸
3楼-- · 2019-04-09 08:13

LINQ to Twitter supports WP7 and has a TweetWithMedia method that works like this:

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

Joe

查看更多
登录 后发表回答