Accessing Reddit top posts using OAuth

Summary

Reddit's public API for top posts can be unreliable for automated scripts, often leading to blocked requests. A robust alternative involves utilizing Reddit's OAuth-protected endpoints, specifically through an Application-Only OAuth flow. This process requires creating a "script" type app on Reddit to obtain a client ID and secret. Developers can then exchange these credentials for an access token by making a POST request to the /api/v1/access_token endpoint with a client_credentials grant type. The acquired access token is subsequently used in a Bearer Authorization header to reliably fetch subreddit top posts from oauth.reddit.com endpoints.

Previously one can use the https://www.reddit.com/r/rprogramming/top.json API to access one subreddit's top posts. This API doesn't require any access token to fetch the data. However, this API may not work all the time. Reddit may block the API's request if it finds that you are using a script or some app which accesses the API now and then. For an app or script which needs to fetch the data routinely, what should we do?

Reddit provides one method which can be used to fetch this kind of top posts data(no need to involve user data) and it requires one to access it with OAuth. In this post, we will demonstrate how to achieve this. This example is using PHP, but it should be very easy to port it with other programming languages.

The API to access programming subreddit top posts is https://oauth.reddit.com/r/programming/top.json. To access it properly, we need an access token, this access token can be generated using OAuth. The API to use to get the access token is https://www.reddit.com/api/v1/access_token. Before calling it, we need to create an app on Reddit. The app type to create is script so that we can access the API at server side without involving complicated user access grant operation since we will not access user data. 

Once the app is created, record down the client_id and client_secret

Next, we will call the access token API with the Application Only OAuth method. This just involves server to server communication without user interaction or authorization. 

Now let's start to make the call. Below is the code:

// vars to get access token
$url ='https://www.reddit.com/api/v1/access_token';
$clientId = '[client_id]'; // from app creation
$clientSecret = '[client_secret]';  // from app creation

// post variables
$fields = array (
    'grant_type' => 'client_credentials'
);

$userAgent = 'pxlet v0.1 by stackoverflooooooow';

// prepare data for post
$fieldString = http_build_query($fields);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode($clientId . ':' . $clientSecret) ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fieldString);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
var_dump($err);

$response = json_decode($response, true);
var_dump($response); // access_token should be here

The response would be something like:

{
    "access_token": Your access token,
    "token_type": "bearer",
    "expires_in": Unix Epoch Seconds,
    "scope": A scope string,
}

Thereafter, this access token can be used to fetch the top posts. The code would be:

$curl = curl_init('https://oauth.reddit.com/r/programming/top.json');
$userAgent = 'pxlet v0.1 by stackoverflooooooow';

curl_setopt( $curl, CURLOPT_HTTPHEADER, array('Authorization: bearer ' . $response['access_token'] ) );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

$response = json_decode($response, true);
var_dump($response); // data should be here

The response should be a JSON data structure contains the top posts of programming subreddit.  

For other data, can refer to API specification on what can be fetched.

OAUTH REDDIT TOP POSTS HOT POSTS

  RELATED

  COMMENTS

0

No comment for this article.