Access Data from Twitter API using R and Python

Michael Galarnyk
Towards Data Science
4 min readOct 29, 2018

--

NOTE: There is an updated version of this tutorial that goes over setting up the Twitter Developer Account and the tweepy library (Python) here.

Using the Twitter API should be an easy thing, but sometimes pictures and simple code can save you that extra 5 minutes. I previously covered how to Access Data from Twitter API using R, but the process has changed as of July 2018.

Things changed in July 2018

This post goes first goes over how to setup your Twitter developer account then goes over how you can search tweets using R and Python.

Setup Twitter Developer Account and App

1-) Create a twitter account if you do not already have one.

2-) Apply for a twitter developer account.

This was made necessary as of July 2018

3-) Enter phone number if you don’t have one associated with your twitter.

4-) Add account details. Click on continue.

5-) Describe in your own words what you are building. Click on continue.

6-) Submit application.

7-) Check your email associated with your twitter and click Confirm your email.

8-) On the welcome screen, click on Create an app.

9-) Fill out your App details and click on Create (its at the bottom of the page). Make sure you don’t try to make a appName that is already taken.

Make sure to make an App name that is not already taken

Review Developer Terms and click on Create.

10-) First, click on Keys and tokens. Second, click on create to get access token and access token secret.

Save your API key, API secret key, Access token, and Access token secret somewhere safe. I should note that you should not try and copy my keys as I regenerated them after this tutorial.

Twitter API

R

If you want to use R, you can use twitteR (make sure you install first install the twitteR package). twitteR is an R package which provides access to the Twitter API. Most functionality of the API is supported, with a bias towards API calls that are more useful in data analysis as opposed to daily interaction. Read the user vignette if you want to learn more about how to use the package for your various needs. The code below uses the Twitter search API.

#install.packages("twitteR")library(twitteR) 
# Change consumer_key, consume_secret, access_token, and
# access_secret based on your own keys
consumer_key <- ""
consumer_secret <-""
access_token <- ""
access_secret <- ""
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
tw = searchTwitter('@GalarnykMichael', n = 25)
d = twListToDF(tw)
As of when this tutorial was written, the standard search API searches against a sampling of recent Tweets published in the past 7 days. Part of the ‘public’ set of APIs. https://developer.twitter.com/en/docs/tweets/search/overview

Python

The code below uses the python-twitter package (you can install using pip install python-twitter) for searching. You can learn how to make your own query here.

import twitter"""
Change ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY and CONSUMER_SECRET
to your own.
"""ACCESS_TOKEN = ''
ACCESS_SECRET = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
t = twitter.Api(consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN,
access_token_secret=ACCESS_SECRET)
results = t.GetSearch(raw_query="q=from%3AGalarnykMichael&src=typd")
As of when this tutorial was written, the standard search API searches against a sampling of recent Tweets published in the past 7 days. Part of the ‘public’ set of APIs. https://developer.twitter.com/en/docs/tweets/search/overview

Conclusion

This tutorial was about getting you started with the Twitter API. If you have any questions or thoughts on the tutorial, feel free to reach out in the comments below or through Twitter. If you want to learn how to utilize the Pandas, Matplotlib, or Seaborn libraries, please consider taking my Python for Data Visualization LinkedIn Learning course.

--

--