In order to be able to use Twitter via a Python script, you must at least have access to the standard Twitter API. You will then need to provide the access tokens and consumer API key in the script so as to gain authorized access to your account. This post shows how one can easily register with Twitter Developers to use the API in Python.
Very self-explanatory as of this point, search for ‘Twitter Developers’ and select the first hit.
Editing source code via Inspect Element
You may notice that the first hit has been marked in yellow and the text is in uppercase. One can actually change a bit of source code while surfing the web thanks to the Inspect Element tool on Google Chrome!
Inspect Element allows users to access and temporarily alter HTML and CSS code in a website. This tool is especially helpful for web developers when testing out their website to see what can be changed, implemented and removed. They can see how the website would look with different styles (background colour as an example) and fix any errors present in their JavaScript code (it also warns you about any missing content referenced in the code) via the Console section as pictured below.
I had referenced a non-existent image and failed to use the correct hide/show toggle function in jQuery, thus triggering the errors above.
Let’s try it out, shall we?
Right-click the first hit and select ‘Inspect’.
Select the line of code representing said hit and edit its HTML code, this will allow us to how the element has been stylized in CSS. This section may seem rather intimidating if you lack knowledge of HTML and CSS. If you are simply interested in registering for Twitter Developers, skip this.
.L20lb is a class assigned to the <h3> element, add another property to it which will change the case of said element to uppercase. Look to the left and notice the difference.
text-transform: uppercase;
Select the <h3> element once more but this time we are going to modify its HTML code in order to highlight the heading via <mark>.
On the right, there is CSS code for the highlight element. You change it to another colour if you like by altering the background-color property. Note that both HTML and CSS does not recognize British spelling. Also remember that each statement must end with a semicolon!
background-color: yellow;
Once you’re done, simply close the Inspect Element tool. Note that the changes committed will be erased once you refresh/close the tab.
Registering for Twitter Developers
Once you have accessed the homepage, select Apply from the navigation bar on the right next to the search functionality and the avatar.
Select ‘Apply for a developer account’ to proceed.
Choose only one option applicable to you.
Give a detailed explanation as to why you wish to use the Twitter API, this may affect whether or not your application will be accepted.
Enable/disable the element according to your needs.
List the functionalities you intend to use and why.
Enable/disable the two elements according to your needs.
Verify that all the input is correct, you have the option to go back and alter the form. Otherwise, select ‘Looks good!’.
Agree to the terms and conditions and select ‘Submit Application’.
You should receive an email prompting you to confirm your email address.
Once you have confirmed your email address, select ‘Create an app’ within the Get Started section.
Since we need to gain access tokens in order to gain access to our Twitter account while running the Python script, we need to create a new application.
Enter the necessary details as instructed.
The first four text fields can be left empty. In the last text field, you have to explain how this application will work and how users can benefit from it.
Heed the above information.
These are the keys and tokens you will need to reference in your Python script. Under any circumstances must you disclose any of these! It is recommended that you regenerate them every now and then.
Now we have created the application. There is nothing left to do on Twitter Developers, we may now create a Python script and take it from there.
Evidence that the solution works
Copy the consumer API keys and access tokens, we will need to assign them to variables in Python.
I created this very short-but-sweet script that will upload a post saying “Welcome to my feed” to my Twitter page. For the sake of keeping things simple, I refrained from using the try-catch block and asking for input.
Press F5 to run, it will prompt you to save the file. Upon saving, it should run. If it works, you should see the following.
That’s all there is to it! Thank you for taking some time out of your day to read this post! I hope it helps you in one way or another.
17-year old software development student with a penchant for programming and strives to go above and beyond to attain more knowledge about the fields of IT.
View more posts