Upload image directly to twitter

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

I ended up using twitter and could update the tweets, but I was not able to upload the image to twitter using wp7?

+5
source share
2 answers

I developed a solution for this using the Hammock.WindowsPhone.Mango library. (TweetSharp internally uses the Hammock library for oAuth and other functions, but I never used TweetSharp or Twitterizer)

I installed the latest version of Hammock from Nuget

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

photoName - ( "e.OriginalFileName" ) photoStream - "e.ChosenPhoto" PhotoChooserTask

4- .AddFile() ( , )

, !

+9

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

0

All Articles