How to Code Your Own Twitter Client in Python Using OAuth « Null Byte :: WonderHowTo

This is my attempt to show people some cool things we can do with programming, and to give a small incentive for people to join the weeklyCommunity Bytes. I wanted to make a tutorial on how to useTwitterfrom the command line with Python. This would have been an easy project to do not too far back, and a good bit of practice for a beginner to programming. It teaches that you can dousefulstuff with programming. However, a while ago Twitter started using something calledOAuthto access the TwitterAPI.OAuth was implemented for added security. Before, anyone could code a program to use Twitter and make it store the Twitter ID and password. Twitter thought it would be a good idea to add OAuth, which creates public keys, much likeSSH, to exchange with the Twitter API so the application is unable to access your password, mitigating the chances of a program stealing user credentials.Anyhow, let's get started.RequirementsTwitteraccountPythoninstalled on your systemStep1Download a Twitter LibraryText in bold is a terminal command for Linux. Windows users, simplydownload Tweepystraight from the repository.To make this as simple as possible, we are going to use a pre-made library that we can call functions from. For this tutorial, I am going to use the Tweepy library. Recoding a Twitter API library from scratch is beyond the scope of this tutorial.Download and Install TweepyYou need to install Tweepy from the Git repo.git clone https://github.com/tweepy/tweepyChange to the new Tweepy directory.cd tweepy/Run the setup with the Python installer.python setup.pyStep2Register Our Application with TwitterWe need to let Twitter know that we are allowing our application to have access to our Twitter account. This will also give us the keys we need.Register our new appLog in to Twitter.Gohereto register your application and fill in the appropriate data on the form.Name the application whatever you would like. Save the data.Leave the browser open with our registry data, we need to create our application now.Exchange the Unique KeysWe need to create a small script that exchanges the keys before we code the actual program. This is done separately because it only needs to be used once, therefore, it would take up space in the finished code. Here is a picture of mine without the keys:Copy the source codehere.Paste the source code in a text document, abd paste your keys in the appropriate field by copying them from your Twitter page that we left open earlier.Save it asauth.py.From a command promt or terminal, type the following command to run the script.python auth.pyFollow the URL that the program gives you when you run it and then enter your PIN into the field.Copy and paste the new keys that it give you into a text document for later use.Step3Create the Command Line ApplicationWe need to take our keys that we saved from the last part and paste them into our bit of Python code. This will ensure that Twitter knows it is an application we have authorized tweeting as us, but we don't have to risk exposing our password to the program (in case it was a 3rd party program).Paste the Keys in Our programWe need to paste all of the corresponding keys in our program. Here is a picture of how it came out:Get the source codehere.Copy and paste your corresponding keys that we saved in between the quotes.Save the document astweet.py.To use the program, execute the following code from the command line.python tweet.py"This is a tweet using Null Byte's tweet.py script."So, join in on our coding sessions to learn how to code simple, yet fun programs like this one on your own!Follow us onTwitter; come chat with us onIRC.Want to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseImage viaunder30ceoRelatedHow To:Reverse Shell Using PythonHow To:The Novice Guide to Teaching Yourself How to Program (Learning Resources Included)How To:Use variables and strings when programming in Python 2How To:Program Your Own Little RAT (Part 2) Getting the Client WorkingHow To:Program Your Own Little RAT (Part 1) Getting the Server WorkingHow To:Hack UnrealIRCd Using Python Socket ProgrammingHow to Train Your Python:Part 1, IntroductionHow To:Enable Code Syntax Highlighting for Python in the Nano Text EditorNews:Learning Python 3.x as I go (Last Updated 6/72012)Community Byte:Coding a Web-Based Password Cracker in PythonCommunity Byte:Coding an IRC Bot in Python (For Beginners)How To:Code a Basic TCP/IP Client & Server Duo in PythonHow To:Shorten URLs from the Command Line with PythonHow To:Make a Gmail Notifier in PythonNews:The Google+ API Has Finally Launched with Read-Only Public DataPygame:All You Need to Start Making Games in PythonCommunity Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingHow To:Send SMS Messages with PythonCommunity Byte:HackThisSite Walkthrough, Part 8 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 7 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 3 - Legal Hacker TrainingHow To:Make a Change-of-IP Notifier in PythonCommunity Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingHow To:Generate Word-Lists with Python for Dictionary AttacksCommunity Byte:HackThisSite Walkthrough, Part 5 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingWeekend Homework:How to Become a Null Byte ContributorCommunity Byte:Hack Our IRC Bot to Issue CommandsCommunity Byte:HackThisSite, Realistic 5 - Real Hacking SimulationsCommunity Byte:HackThisSite, Realistic 2 - Real Hacking SimulationsHow To:Encrypt And Decrypt Text In PythonCommunity Byte:HackThisSite Walkthrough, Part 4 - Legal Hacker TrainingGoodnight Byte:Coding a Web-Based Password Cracker in PythonNews:A Basic Website Crawler, in Python, in 12 Lines of Code.Community Byte:HackThisSite, Realistic 4 - Real Hacking SimulationsCommunity Byte:HackThisSite, Realistic 3 - Real Hacking SimulationsCommunity Byte:HackThisSite, Realistic 1 - Real Hacking SimulationsUDP Flooding:How to Kick a Local User Off the NetworkWeekend Homework:How to Become a Null Byte Contributor (1/29/2012)Goodnight Byte:Coding an IRC Bot in Python (For Beginners)