Print

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

Click here to install the API Connector add-on from the Google Marketplace.

Part 1: Create a Reddit App

  1. Log into Reddit and navigate to https://www.reddit.com/prefs/apps
  2. Click the "Create app" or "Create another app" button
  3. 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.
    reddit-createapp
  4. You'll now see your new application listed under developed applications. Click edit.
    reddit-editapp
  5. The form will display your client app and client secret. Copy these down, we'll use them shortly.
    reddit-credentials
  6. 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.

We'll refer to these encoded credentials as your_encoded_credentials and use them in the next part.
It's possible but unlikely Reddit will block your initial API requests with an error. If so, you may need to register to use the API. In that case, fill out the registration form. Select "I’m a Developer" and "I want to register to use the Reddit API."

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"}
reddit-tokenrequest

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.

  1. Open up Google Sheets and click Extensions > API Connector > Open > Create request.
  2. In the request form enter the following. Substitute your own subreddit and keyword of interest.
    • ApplicationCustom
    • MethodGET
    • Request URLhttps://oauth.reddit.com/r/wallstreetbets/search.json?q=tesla&type=link&restrict_sr=on&limit=100
    • Headers:
      • Authorization: Bearer +++Token!A2+++
  3. Create a new tab and click Set current to use that tab as your data destination.
  4. 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.
reddit-pagination

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
    reddit-pagination-cursor

Part 5: API Documentation and Resources

Leave a Comment

Jump To