Jun 26, 2012

Upload image to Twitter from a Windows Phone Application

Here is a blog post for Windows Phone Application developers. In this blog post I will show you how to implement photo upload feature for Twitter from your Windows Phone Application.

If you want to implement basic text sharing functionality for Twitter(No photo or customized sharing), I would suggest to use ShareStatusTask and ShareLinkTask which makes the work much easier for the developers.But if you want to implement some customized sharing like photo upload, you need to put in a little effort. In this blog post, I will demo the photo upload feature with the help of the Hammock library.


Step 1: Install the latest version of Hammock library from Nuget, which helps in the implementation of Oauth process.


Step 2: Refer this blog post on How to implement Oauth authentication process for Twitter using hammock (The post may not be directly compatible with latest version of Hammock, check for any inconsistency).


Step 3: Obtain the required oauth tokens as specified in the above blog post from step 2.


Step 4: Post a sample tweet as specified in this blog post Post a Tweet on a User’s behalf

Step 5: After successfully completing all the above steps, now here is the code to upload a photo along with a tweet..
(Check the Twitter documentaion on Status Update with Media)

        private void PostImageButtonClick(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
            photoChooserTask.Show();
        }

        void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                uploadPhoto(e.ChosenPhoto, e.OriginalFileName);
            }
        }

        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", textBoxNewTweet.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
                }
        }



and the 4th parameter for .AddFile() should be taken care of(I have not considered other formats while doing this sample, you have to take care in your apps)
Note: If you like this Blog Post, Plz share this and Plz Upvote my answer in StackOverflow
View Kishor Chandra's profile on LinkedIn

3 comments:

  1. HI i know this is an older post but does this still stand for update status and image upload for twitter? I'm using tweetsharp and i tried your code and everything stands except for this:

    squiggly line here: Credentials = credentials <----

    (local variable) var credentials
    C#Unknown type of variable'credentials'

    ReplyDelete