Sending a tweet through Birdhouse and OAuth

For our final recipe, we're going to put everything together and, using a combination of our previous recipe's "image post" functionality and the Birdhouse API, post a tweet containing a message from our app and the image URL that accompanies it.

Note

Complete source code for this recipe can be found in the /Chapter 5/Recipe 8 folder.

Complete source code for this entire chapter can be found in the /Chapter 5/PhotoShare folder.

How to do it…

Alter the postToTwitter function in your existing app.js file to match the following code by adding our new BH.tweet method call. If you already integrated the photo upload code from the previous recipe, then this source code snippet should appear in the xhr.onload() event handler:

BH.tweet(txtMessage.value + ' ' + this.responseText, 
  function(){
    alertDialog = Ti.UI.createAlertDialog({
      message:'Tweet posted!'
    });

    alertDialog.show();
});

Try running your application now in the simulator, and after authorization to Twitter (if necessary) your new tweet should get posted directly from your app! Go to http://twitter.com/yourusername to see the tweet posted to your timeline (as seen next). Clicking on the link should then load your uploaded photo!

How to do it…

How it works…

This recipe is very simple, as all of the hard work in connecting to Twitter and uploading an image to our server has been taken care of. Therefore pushing our final message via Birdhouse to the Twitter API is actually very easy. Here we are calling the Birdhouse tweet() function, which accepts both a message (as a string) and a second parameter that acts as the event handler for the response from Twitter. You can also use this tweet function outside of posting an image or file to the server. Try posting a simple message tweet without the image and code and you'll see it works just as well!

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.216