When working with the Twitter API, there are numerous reasons you may want to transfer a response payload to Google Sheets. Google sheets are great for basic data analysis and sharing results quickly. This can be particularly useful for ad hoc analysis where you are working with stakeholders from different backgrounds. In a previous blog post, we showed you how to use a Tweet formula to help you work more dynamically with Tweets inside of a Google Sheets workbook. This blog post walks you through how to use Python to make a request to the Twitter API v2 and send the response to a Google Sheets spreadsheet.
Before you can use the Twitter API v2, you need a developer account. Once you have an approved developer account, you need to create a Project, and an App inside of that Project. For more information about how to get started with the Twitter API, check out this blog post and the getting started section of our documentation.
For this code sample, you’ll use Python 3. You also need to need to connect to the Google Drive API. If you need any help getting set up be sure to check out this helpful resource.
To get set up, install pandas, gspread, requests, and oauth2client. You’ll use pandas to transform the JSON payload response from the Twitter API to a tabular format, gspread to connect to Google Sheets, requests to connect to make an HTTP request to the Twitter API, and oauth2client to help with authentication using service accounts. You’ll also be importing os, but you do not need to install this first since this is included in the standard
Setting your environment variable for your bearer token
In order to connect to the Twitter API, you must first authenticate to the API. You can do this with your App’s bearer token. You obtain your bearer token in your App’s keys and tokens section inside of the developer portal. To ensure you don’t accidentally share your bearer token you can set an environment variable. To do this, inside of your terminal run the following and be sure to replace where it says your-bearer-token with your own.
Inside of your code editor, create a function called connect_to_twitter where you can get your bearer token and return a header that you will pass into your HTTP request to the Twitter API.
Making a request to the Twitter API
You’ll be making a request to the Tweet lookup endpoint which allows you to get back Tweets that match a given ID. By default, this endpoint will return only the fields for idwhich contains the Tweet ID, and text which returns the text of a Tweet. You can also add additional fields to the object that gets returned. For the example below, you’ll want to specify that you want to include fields for author_id, created_at, and lang in the params variable.
Next, make a function called make_request, which passes in the authentication header you just created. Now, the URL will make a request to include the fields you want returned and the Tweet IDs for the Tweets you are looking to get more information about. Using the requests library, make a GET request to the URL variable, pass in your authentication header, and specify that you want the request to return a JSON payload.
Parsing your JSON payload
The response that gets returned will be inside of a JSON payload, that looks similar to the following.
The information for the Tweets you requested is in an object called data, but it’s not exactly a format that’s friendly for spreadsheets. To get it ready for rows and columns, create a function called make_df which will turn your JSON payload into a pandas data frame, which is similar to a spreadsheet in structure because they are both tabular in nature. If you are using an expansion or an endpoint such as a recent search that has multiple objects, you may need to normalize your JSON before you are ready to convert your object.
Authenticating to the Google Sheets API
You’ll be using a service account to connect to the Google Sheets API. A service account is an account that belongs to your application in a way that is similar to how a user account would interact with a product. To authenticate to the Google Sheets API, you will first need to set the scope you will be working with, and pass in your service account credential information.
Your service account also has an email address. You will need to update your share settings of the spreadsheet you’d like to modify to include the service account, just like you would with a coworker or a friend by adding that email address to where it says “add people or groups” in share settings of a Google Sheets spreadsheet.
Tying it together
Now, create a main function to call all of the functions we’ve written so far. You’ll want to create a variable called headers which is where you call the connect_to_twitter function to set your authentication header. The variable response is where you call the make_request function and pass in the response variable. In the df variable, create the data frame for the JSON response object you get back from the Twitter API. Next, save a variable called credentials and call the authenticate_to_google function to set your service account credentials. The next step is to create a variable called gc that authorizes your credentials.
The variable workbook is where you set the value of the spreadsheet you are working with, by opening it with your spreadsheet key. Let’s say you have a Google Sheet spreadsheet with the following url.
The spreadsheet key for this example would be skosdo;gaetgiwoesofgkd45. In the code snippet below you’d want to replace spreadsheet_id with skosdo;gaetgiwoesofgkd45.
You will also want to set the sheet you’re working on, for this example it’s Sheet1 but this can be any sheet you have in your spreadsheet. You’ll also need to specify the cell value or range you want to update, in this example you’ll start with A1 which is the first cell. Finally, to add the values of your data frame to update your spreadsheet with data from the Twitter API.
You can check out the full version of the code on our GitHub. You can expand upon this code sample by using Google Script once you have data from the Twitter API sent to a Google Sheet. Hopefully this blog post can serve as a starting point for working with both Twitter Data and the Google Sheets API. Be sure to let us know on the forums if you run into any troubles along the way or Tweet us at @TwitterDev if this inspires you to create anything.