First steps with the Twitter Academic API
Finally, Twitter released the academic track, a new API endpoint just for researchers! The application is already open, and I am one of the lucky guys with approved access.
However, it is relatively new and, therefore, it is not easy to find many tutorials or related materials online. I want to change this and start with a quick-start on loading historic tweets (incl. package suggestion and configuration). I highly recommend the python package TwitterAPI. It already provides support for the new authentication mechanism and all endpoints of the academic track.
- First of all, you have to install the package
pip install TwitterAPI
- Before we can query the API, we have to authenticate. If you don’t have created an application (app) yet, please create a new one first. You find your application’s API key as well as the secret in the Twitter developer portal (Projects & Apps -> your project -> your application; there is a tab named “Keys and tokens” directly below the application’s name). Such that the package uses the right authentication method, you have to set the auth_type to ‘oAuth2’. Besides, we are going to use the API version 2, which we provide as an argument.
from TwitterAPI import TwitterAPI
api_key = "<your key>"
api_secret_key = "<your secret>"
api = TwitterAPI(api_key, api_secret_key, auth_type='oAuth2', api_version='2')
- As soon as you expect many results, it is wise to use the TwitterPager, which loads all tweets batch-wise. Please take a look at the query builder documentation for more information about the topic. The argument max_results defines the batch-size, and 500 is the API’s maximal value. You can find additional API parameters in the documentation right here.
from TwitterAPI import TwitterPager
r = TwitterPager(api, 'tweets/search/all', {
'query': 'your query',
'max_results': 500,
})
for item in r.get_iterator():
# voila !
print(item)