I see you’re looking for a way to fetch Reddit posts—let’s take care of that!
I’ve simplified the process for you. Since OAuth authorization is already integrated into our platform, you can add it as a custom parameter, which REALLY streamlines the entire process.
Here’s what I came up with:
Using the thread ID and subreddit, I successfully retrieved the title, thread content, and comments.
The code is fully functional, so all you need to do is adjust it to suit your needs.
Hope this helps!
As for the code itself, here’s what I’ve done:
/** @CustomParams
{
"subreddit": {
"title": "Subreddit",
"key": "subreddit",
"description": "The name of the subreddit to fetch the post from",
"type": "string"
},
"post_id": {
"title": "Post ID",
"key": "post_id",
"description": "The ID of the post to fetch",
"type": "string"
},
"oauth_reddit": {
"title": "Reddit OAuth",
"key": "oauth_reddit",
"description": "Select your Reddit OAuth token",
"type": "connection",
"required": true
}
}
*/
import axios from 'axios';
export default async function run({ data }) {
const subreddit = data.subreddit;
const post_id = data.post_id;
const oauth_reddit = data.oauth_reddit;
if (!subreddit || !post_id || !oauth_reddit) {
throw new Error('Missing required parameters: subreddit, post_id, or oauth_reddit');
}
const url = `https://oauth.reddit.com/r/${subreddit}/comments/${post_id}`;
try {
const response = await axios.get(url, {
headers: {
'Authorization': `Bearer ${oauth_reddit}`,
'User-Agent': 'YourApp/0.1 by yourusername'
}
});
return response.data;
} catch (error) {
throw new Error(`Failed to fetch Reddit post: ${error.message}`);
}
}
BTW, you can log in to your Reddit account through the “Authorization” tab or add the Reddit node and authorize through that.
Once done, you’ll be able to select this authorization in your JavaScript.
