Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload image directly to twitter

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?

like image 401
Gokoulane Ravi Avatar asked May 03 '12 11:05

Gokoulane Ravi


People also ask

Can you upload JPEG to Twitter?

Check your file type.Twitter supports JPEG, GIF, and PNG file formats.

Can you post an image on Twitter?

Uploading a photo on Twitter is simple. Tap the photo icon when you're composing a Tweet to find one you've saved, or use the GIF icon to search the GIF library. You can upload up to 4 photos per Tweet.

Why can't I post my pictures on Twitter?

Supported Formats. Images uploaded to Twitter must be smaller than 3MB in size and saved as a GIF, JPEG or PNG file. The BMP, TIFF and animated GIF file formats are not supported by Twitter. Note that you can only upload one image per tweet, whether you are using the main Twitter website or a third-party application.

How do I upload pictures to Twitter from my computer?

Log in to your account on Twitter. Use the compose tweet button to create a new tweet. Click the add media button. Select a photo, video, or GIF from your computer, and click open.


1 Answers

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!!

like image 87
nkchandra Avatar answered Sep 26 '22 01:09

nkchandra