Chapter 3. Setting Up to Create Twitter Apps

In This Chapter

  • Signing up for Twitter

  • Establishing version control

  • Creating your first Twitter app

In this chapter, you create your first Twitter application. Don't get too excited. The example Twitter app in this chapter is not the next "killer app." But it does illustrate how to work with the Twitter API. I also cover the importance of version control software, which helps you manage your code files

Create Your Developer Account

Before you can start writing Twitter applications, you need a Twitter account. I assume that since you are interested in developing Twitter apps, you must have some experience with Twitter using a personal account. If not, no worries; now is an excellent time to create your personal account. However, you will also need an account dedicated to your application.

Your application needs a Twitter account to authenticate with Twitter's API. You can use your personal account to do this, but if any problems arise with your application, your personal account may be penalized. There's no reason to take that risk.

Creating a Twitter account for development is easy:

  1. Go to https://twitter.com/signup and fill out the provided fields.

  2. Twitter asks if you want to look up any of your e-mail contacts on Twitter.

    I don't recommend this option.

    Note

    Twitter recommends a list of popular Twitter accounts for you to follow. Since this account is dedicated to your application and is not used for light reading, I recommend not following any of the suggested users.

  3. Click the Finish button, and you're done creating your account.

The Importance of Version Control

Tip

Use a hosted version control repository instead of installing and managing your own server. You can easily get up and running in minutes with version control by going to a site like Beanstalk (http://beanstalkapp.com), Unfuddle (http://unfuddle.com), or GitHub (http://github.com), signing up for a free account, and using their online repository URL and Web-based administration tools.

Version control software" makes programming more manageable in a few different ways:

  • Track file changes over time.

    Tip

    This is useful to development teams because they can resolve conflicts that arise when two or more developers simultaneously update the same line of code. When this happens, version control software is used to inspect the differences and correctly merge the files.

  • Compare the differences between the current version of a file and an older version of the same file.

  • Roll a file back to a previous version.

  • This is a lifesaver if you accidentally introduce a debilitating bug into your program because you can simply roll your code back to the last known working version while you chase down the new bug. Even if you're coding by yourself and you're not on a team, version control can give you the security to try new things with your code because you can always roll any changes back if you break something.

If you're new to version control, I recommend signing up for a free Subversion account with Beanstalk.. Subversion is a widely adopted version control system and it's easy to learn. By using Beanstalk, you don't have to set up or manage your own Subversion servers. They do that for you. To create your free Beanstalk Subversion account, sign up at https://signup.beanstalkapp.com/accounts/new?plan=free.

Hello Twitter!

It's time to write your first Twitter app! Writing the app is actually a lot easier than you might think. Follow these steps:

  1. Create a new file titled HelloTwitter.html and save it anywhere on your local hard drive.

    Tip

    This is a great chance to try a version control program, like Subversion.

  2. In the new file, add the HTML from Listing 3-1.

    Example 3.1. HelloTwitter.html

    <html>
       <head>
          <title>Hello Twitter!</title>
       </head>
       <body>
    
          <form name="input"
                action="http://username:password@twitter.com/statuses/update.xml"
                method="post">
             Tweet: <textarea cols="40" rows="3" name="status">Hello Twitter!</
                  textarea><br />
    
             <input type="submit" value="Submit" />
          </form>
    
       </body>
    </html>

    Listing 3-1 is an HTML form that posts the status message "Hello Twitter!" to your Twitter account:

    1. The form's action performs an HTTP POST to the address twitter.com/statuses/update.xml.

    2. The post address passes your Twitter login credentials to the API method.

    3. The field named "status" allows you to input the text of your status update.

  3. Replace username and password in the form action (seen bolded in Listing 3-1) with your application's Twitter account credentials (as created previously in this chapter).

    Warning

    Do not upload this file to the Internet or use this example for production code. Your username and password are clearly viewable in this file, and that is a major security hole. You do not want the general public to have access to your Twitter account credentials.

  4. Open this file locally on your computer with your Web browser (I recommend using Firefox or Internet Explorer to properly view the results); then click the Submit button.

    Tip

    Some browsers don't display XML in or Internet Explorer to view XML files. If you are using Safari or Google Chrome, you need to view the source code of the response page to see code that looks similar to Listing 3-2. You can view the source code by right clicking on the results page, then selecting "View Source" in the popup menu.

    After you click the Submit button, Twitter responds with the details of the status message you just submitted. The status message details include the details of the user account. The response is an XML file that should look similar to Listing 3-2.

    Example 3.2. Twitter Status Update XML Response

    <?xml version="1.0" encoding="UTF-8"?>
    <status>
       <created_at>Sat Sep 19 04:19:17 +0000 2009</created_at>
       <id>4096090186</id>
       <text>Hello World!</text>
    
       <source>
          <a href="http://apiwiki.twitter.com/" rel="nofollow">API</a>
       </source>
       <truncated>false</truncated>
       <in_reply_to_status_id/>
       <in_reply_to_user_id/>
       <favorited>false</favorited>
       <in_reply_to_screen_name/>
    
       <user>
          <id>75155000</id>
          <name>testfordummies</name>
          <screen_name>testfordummies</screen_name>
          <location/>
          <description/>
          <profile_image_url>
          http://s.twimg.com/a/1253301564/images/default_profile_4_normal.png
          </profile_image_url>
          <url/>
          <protected>false</protected>
          <followers_count>0</followers_count>
          <profile_background_color>9ae4e8</profile_background_color>
          <profile_text_color>000000</profile_text_color>
          <profile_link_color>0000ff</profile_link_color>
          <profile_sidebar_fill_color>e0ff92</profile_sidebar_fill_color>
          <profile_sidebar_border_color>87bc44</profile_sidebar_border_color>
    <friends_count>0</friends_count>
          <created_at>Fri Sep 18 00:19:49 +0000 2009</created_at>
          <favourites_count>0</favourites_count>
          <utc_offset/>
          <time_zone/>
    
          <profile_background_image_url>
          http://s.twimg.com/a/1253301564/images/themes/theme1/bg.png
          </profile_background_image_url>
          <profile_background_tile>false</profile_background_tile>
          <statuses_count>3</statuses_count>
          <notifications>false</notifications>
          <verified>false</verified>
          <following>false</following>
       </user>
    </status>
  5. Open the Twitter account and verify that the form updated your status.

Congratulations! You've written your first Twitter application!

Tip

Chapter 4 covers several areas of the Twitter application ecosystem and will provide you with inspiration and a general idea of what you can do with the API.

..................Content has been hidden....................

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