API Connector Documentation
Import Reddit Data to Google Sheets
In this guide, we’ll walk through how to pull data from the Reddit API directly into Google Sheets, using the API Connector add-on for Sheets.
While it's possible to access Reddit's public API without authentication, Reddit rate limits these requests, which can block even light usage when sending requests through Google Sheets' shared pool of IP addresses (in those cases you'll see a 403 error). Therefore we'll connect through a personal OAuth token to avoid that.
Contents
- Before You Begin
- Part 1: Create a Reddit App
- Part 2: Get your Reddit Token
- Part 3: Pull Reddit API Data into Sheets
- Part 4: Handle Pagination
- Part 5: API Documentation and Resources
Before You Begin
Click here to install the API Connector add-on from the Google Marketplace.
Part 1: Create a Reddit App
- Log into Reddit and navigate to https://www.reddit.com/prefs/apps
- Click the "Create app" or "Create another app" button
- Fill out the "create application" form. Give the application a name, set the type to "script" and enter a value for the required redirect uri (we won't be using a redirect URL so the value doesn't matter). Click create app.
- You'll now see your new application listed under developed applications. Click edit.
- The form will display your client app and client secret. Copy these down, we'll use them shortly.
- One last step: Because the Reddit API requires Basic Authentication, we need to encode the client ID and client secret to base 64. You can do this by entering those values into this form in the format
id:secret
(i.e. first your client ID, then a colon, then your client secret). The encoding script runs in your browser, and none of your credentials are seen or stored by this site.
your_encoded_credentials
and use them in the next part.Part 2: Get your Reddit Token
We'll get a token using the password grant flow as described here. API Connector's OAuth manager doesn't have native support for the password flow, so we'll configure the request manually as follows:
- Application:
Custom
- Method:
POST
- Request URL:
https://www.reddit.com/api/v1/access_token
- Headers:
Authorization
:Basic
your_encoded_credentials
- Request body:
{"grant_type":"password","username":"your_reddit_username","password":"your_reddit_password"}
To follow along more easily, set the destination sheet to a tab called Token. Pay attention to the field marked in purple above -- that's your access token! We'll use it for all subsequent requests. It only lasts for for 86400 seconds (24 hours), but you can always run this request again to get a new one.
Part 3: Pull Reddit API Data into Google Sheets
You can fetch data for any of the endpoints shown in the documentation, but for this example request, we'll search a subreddit for a keyword. The base domain here is oauth.reddit.com
, not www.reddit.com
.
- Open up Google Sheets and click Extensions > API Connector > Open > Create request.
- In the request form enter the following. Substitute your own subreddit and keyword of interest.
- Application:
Custom
- Method:
GET
- Request URL:
https://oauth.reddit.com/r/wallstreetbets/search.json?q=tesla&type=link&restrict_sr=on&limit=100
- Headers:
Authorization
:Bearer
+++Token!A2+++
- Application:
- Create a new tab and click Set current to use that tab as your data destination.
- Name your request and click Run. A moment later you’ll see public Reddit data populate your sheet.
Note that the Header references the token value returned by the previous request. This way, it doesn't need to be updated when you refresh your token.
Part 4: Handle Pagination
Note that the Reddit limits the number of records returned in each request. By default, only 10 records will be returned unless you set the limit
parameter to 100 as shown in the example above.
If you want to retrieve more than 100 records, you'll need to paginate through as described in their documentation.
With API Connector, you can handle this automatically through pagination handling, like this:
- Pagination: cursor
- Next token parameter: after
- Next token path: data.after
- Run until: choose when to stop fetching data
Part 5: API Documentation and Resources
- Official API documentation: https://www.reddit.com/dev/api/
- You can also try out unofficial Reddit APIs like https://pullpush.io/.