Creating a Twitter application

In this section, we are going to create our first Twitter application so we can consume the Twitter REST API. You will need to create an account if you don't already have one. If you are not using Twitter, I would strongly recommend it; it is a great way of getting up-to-date with all the news and what is going on in the development world, and it is a great way of making new friends in the Python community.

After you create an account, head over to https://apps.twitter.com/, sign in with your login credentials, and you will land on a page where you can see a list of apps that you have already created (the first time, you will probably have an empty list of apps), and on the same page you will have the possibility of creating new apps. Click on the Create new app button in the top-right corner and it will open up the following page:

In this form, there are three fields that are required—name, description, and website:

  • Name: This is the name of your application; it is also the name that will be presented to the users of your application when performing authorization. The name doesn't need to follow any specific naming convention, you can have anything you want.
  • Description: As the name suggests, this is the description of your application. This field will also be presented to the users of your application, so it is good to have nice text describing your application. In this case, here we don't need much text. Let's add Application to cast votes on Twitter using hashtags.
  • Website: Specify your application's website; it is also going to be presented to the users during authorization and it is the site where your users can go to download or get more information about your application. Since we are in the development phase, we can just add a placeholder such as http://www.example.com.
  • Callback URL: This works the same way as the callback URL in the previous application (the Spotify Terminal app) in the previous chapter. It is a URL that Twitter will call to send the authorization code. It is not a required field but we are going to need it, so let's go ahead and add ;http://localhost:3000/callback.

After filling in all the fields, you just need to check the Twitter Developer Agreement and click the Create your Twitter application button.

If everything went well, you will be directed to another page where you can see more details of your newly created application. Just below the name of the application, you will see an area with tabs that shows settings and different pieces of information about the application:

On the first tab, Details, we want to copy all the URLs that we are going to use to perform the authentication. Scroll down to Application settings, and copy Request token URL, Authorize URL, and Access token URL:

Great! Now let's head over to the Keys and Access Tokens tab and copy Consumer Key and Consumer Secret:

Now that we have copied all the necessary information, we can create a configuration file that is going to be used by our application. It is always good practice to keep all this in a configuration file so we don't need to hardcode those URLs in our code.

We are going to add the consumer key and consumer secret to a configuration file in our project; as the name suggests, this key is  secret so if you are planning to create a repository for your code in a service such as GitHub, make sure to add the configuration file to the .gitignore file so the keys are not pushed to the cloud repository. Never share these keys with anyone; if you suspect that someone has the keys, you can generate new keys for your application on the Twitter app's website.