Refresh Your Access Token

Refresh your access token if it has expired.

The access token we received is valid for 24 hours only, and we need to update it afterward. We can, however, use the refresh token to get a new access token, as shown in the widget below.

Press + to interact
// Defining import libraries here
import fetch from "node-fetch";
// Define API key here
const CLIENT_ID = "{{CLIENT_ID}}";
const CLIENT_SECRET = "{{CLIENT_SECRET}}";
// Creating a basic auth token using account ID and license key
const authToken = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString(
"base64"
);
const encoded = authToken.toString("base64");
// Define endpoint URL here
const endpointUrl = new URL("https://www.reddit.com/api/v1/access_token");
// Define Header Parameters here
const headerParameters = {
Authorization: `Basic ${encoded}`,
contentType: "application/json",
};
// Define Body Parameters here
const bodyParameters = new URLSearchParams({
grant_type: "refresh_token",
client_id: "qwnrWzLI75jlYuAVN2ptcg",
client_secret: "HDTnQjO10YwPRKr0epzP0BJj92dyEQ",
refresh_token: "{{REFRESH_TOKEN}}",
});
// Setting API call options
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
// Function to make API call
async function accessTokenUpdation() {
try {
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
accessTokenUpdation();