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