xdevplatform / Twitter-API-to-Google-Sheets

A code sample that allows you to send a payload from the Twitter API to Google Sheets.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sending a response from the Twitter API to Google Sheets

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. Check out our step by step tutorial on the same subject.

Setting up

Before you can use the Twitter API v2, you need an approved 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 library of Python.

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.

export BEARER_TOKEN='your-bearer-token'

Contributing

We welcome pull requests that add meaningful additions to these code samples, particularly for languages that are not yet represented here.

We feel that a welcoming community is important and we ask that you follow Twitter's Open Source Code of Conduct in all interactions with the community.

License

Copyright 2020 Twitter, Inc.

Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0

About

A code sample that allows you to send a payload from the Twitter API to Google Sheets.

License:Apache License 2.0


Languages

Language:Jupyter Notebook 97.0%Language:Python 3.0%