This post is a static and abbreviated version of this interactive tutorial on using R for social data analytics.
What is Twitter API? API (Application Programming Interface) is a marketplace of data. Twitter makes some of its data publically available and free of access. To obtain the data, you must register a client app and complete the authentification process. Here you will learn how to connect to the Twitter API with pre-obtained credentials: consumer key, consumer secret, access token, and access secret. To authenticate Twitter API in the library rtweet, run the following code:
mytoken <- create_token(
app = "", #enter the app name
consumer_key = "", #enter consumer key
consumer_secret = "", #enter consumer secret
access_token = "", #enter access token
access_secret = "") #enter access secret
Test if we can connect to the API by searching 100 recent tweets that contain a keyword of your interest.
tweets <- search_tweets("#privacy", n = 100, token=mytoken)
The Twitter API as you know it may not be here forever
It is difficult to keep up with where things are at the Twitter API as its API has undergone a number of big changes over the past years. The current version of the Twitter API is more restrictive than ever. You might have heard a thing called rate limit, meaning there is a limit on how many times you can make a request for data within a 15-minute window (see https://developer.twitter.com/en/docs/basics/rate-limiting.html). Moreover, historical tweets (tweets sent over a week ago) are NOT available. You must purchase historical tweets through a paid Twitter API subscription (see pricing at https://developer.twitter.com/en/account/subscriptions) or third-party data vendors (e.g., GNIP).
The increasingly restrictive API is partly a result of public and regulatory pressures after the Cambridge Analytica scandal and all the talks about foreign interference in elections through social media. A more privacy-minded Twitter is good news for consumers. But whether or not its latest API policies can actually protect users is up to debate. Twitter’s recent API change has made some data analytics companies’ business model obsolete (including one company launched by a former UMass professor in Amherst, MA). Also, there is a growing consensus within the researcher community that Twitter should have a more lax API policy for academic researchers.
If you are interested in the debate, here are some good reads: