Reputation: 41
I'm currently working on integrating Reddit's API into my application, and I'm running into an issue when trying to submit a post using the /api/submit endpoint. I have already ensured that my OAuth token includes the necessary scopes: identity, submit, and flair.
The Problem: Whenever I try to submit a post using the /api/submit endpoint, I receive a 403 Forbidden response with the message "Blocked." Token and Scopes: I've ensured that my OAuth token includes the necessary scopes (identity, submit, flair), and other API endpoints, such as fetching user data and subreddit information, work perfectly fine with the same token.
User-Agent: I'm using a unique and descriptive User-Agent string as recommended.
I'm able to successfully use other endpoints with the same access token without any issues. My application is registered and follows Reddit's API guidelines and rate limits.
export const submitRedditPost = async (req, res) => {
logInfo(`Attempting to post on Reddit for user ${req.userId}`, path.basename(__filename), submitRedditPost);
const {
subreddit, title, text, kind = 'self', url = "", nsfw = false, spoiler = false, sendreplies = true, flairId, flairText,
} = req.body;
const { Reddit_accessToken } = req.body;
const modhash = req.headers['x-modhash'] || '';
try {
const params = new URLSearchParams({
api_type: 'json',
sr: subreddit, // Only include subreddit if present
title: title,
kind: kind,
nsfw: nsfw,
spoiler: spoiler,
sendreplies: sendreplies,
});
if (kind === 'self') {
params.append('text', text); // Add text for self-posts
} else if (kind === 'link' && url) {
params.append('url', url); // Add URL for link posts
}
if (modhash) {
params.append('uh', modhash);
}
if (subreddit && flairId && flairText) {
params.append('flair_id', flairId);
params.append('flair_text', flairText);
}
console.log(params)
const response = await fetch('https://oauth.reddit.com/api/submit', {
method: 'POST',
headers: {
'Authorization': `Bearer ${Reddit_accessToken}`,
'User-Agent': process.env.USER_AGENT,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: params.toString(),
});
if (!response.ok) {
const contentType = response.headers.get('content-type');
# const errorText = contentType && contentType.includes('application/json')
? await response.json()
: await response.text();
logError(`Failed to post on Reddit: ${response.status} - ${response.statusText} - ${JSON.stringify(errorText)}`, path.basename(__filename));
return res.status(response.status).json({ message: `Failed to post on Reddit: ${response.statusText}`, error: errorText });
}
const responseData = await response.json();
console.log(`Response Data: ${JSON.stringify(responseData)}`);
if (responseData && responseData.json && responseData.json.errors && responseData.json.errors.length > 0) {
logError(`Reddit API error: ${JSON.stringify(responseData.json.errors)}`, path.basename(__filename), submitRedditPost);
return res.status(400).json({ message: "Error from Reddit API", errors: responseData.json.errors });
}
logInfo(`Successfully submitted post to Reddit: ${responseData.json.data.url}`, path.basename(__filename), submitRedditPost);
res.status(201).json({ message: "Post submitted successfully", url: responseData.json.data.url });
} catch (error) {
logError(`Error submitting post to Reddit: ${error.message}`, path.basename(__filename));
res.status(500).json({ message: "Internal server error", error: error.message });
}
};
`
Has anyone faced a similar issue or could provide any insights on what might be causing this "Blocked" error specifically on the /api/submit endpoint? Any help would be greatly appreciated!
Upvotes: 0
Views: 125