How to use Twitter’s API. Why use an API? API stands for... | by Robert Miller | Towards Data Science

Why use an API? API stands for Application Programming Interfaces (APIs) and they allow you to access resources only available on the server. Lets learn how to use twitters API.
Firstly you will need a twitter account with your phone number attached for verification purposes. You will then need to go to apps.twitter.com and create app so we can reference the coresponding keys Twitter generates for this app. These are the keys that we will use with our application to communicate with the Twitter API.
Now going into the code, we will need to install python-twitter API library
!pip install python-twitter
Make sure you understand Twitters rules for using their API, if you do not follow their rules they can block you from accessing data. Here is a link to some of their rate limits https://dev.twitter.com/rest/public/rate-limiting
Now that we have our app, we have keys associated with the app.
consumer_key — Find this in your app page under the “Keys and Access Tokens”
consumer_secret — Right under consumer_key in the “Keys and Access Tokens” tab
access_token_key — You will need to click the “generate tokens” button to get this
access_token_secret — Also available after “generate tokens” is pressed
We will plug those specific keys into our function below
import twitter, re, datetime, pandas as pdclass twitterminer():request_limit   =   20        api             =   False    data            =   []        twitter_keys = {        'consumer_key':       , #add your consumer key        'consumer_secret':     , #add your consumer secret key        'access_token_key':    , #add your access token key        'access_token_secret':  #add your access token secret key    }        def __init__(self,  request_limit = 20):                self.request_limit = request_limit                # This sets the twitter API object for use internall within the class        self.set_api()            def set_api(self):                self.api = twitter.Api(            consumer_key         =   self.twitter_keys['consumer_key'],            consumer_secret      =   self.twitter_keys['consumer_secret'],            access_token_key     =   self.twitter_keys['access_token_key'],            access_token_secret  =   self.twitter_keys['access_token_secret']        )def mine_user_tweets(self, user=" set default user to get data from", mine_retweets=False):statuses   =   self.api.GetUserTimeline(screen_name=user, count=self.request_limit)        data       =   []                for item in statuses:mined = {                'tweet_id': item.id,                'handle': item.user.name,                'retweet_count': item.retweet_count,                'text': item.text,                'mined_at': datetime.datetime.now(),                'created_at': item.created_at,            }                        data.append(mined)                    return data
We then need to instantiate our class to be able to use our functions above.
mine = twitterminer()
Lets use the API to get some tweets from Donald Trumps twitter
# insert handle we liketrump_tweets = miner.mine_user_tweets("realDonaldTrump")trump_df = pd.DataFrame(trump_tweets)
We have now successfully pulled tweets from Donald Trumps twitter onto our local machine to analyze. Good luck gathering your data.