async function commentOnRedditPost(postId, commentText, accessToken) {
const url = `https://oauth.reddit.com/api/comment`;
// const params = new URLSearchParams();
console.log("access_token:", accessToken, postId, commentText);
// params.append("parent", postId); // 'thing_id' is used instead of 'parent'
// params.append("text", commentText);
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
Accept: "text/html",
"User-Agent": "profullstack comment bot v1.0",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "cross-site",
},
mode: "cors",
body: JSON.stringify({
api_type: "json",
thing_id: postId,
text: commentText,
}),
});
if (!response.ok) {
console.error(await response.text());
}
return response.json();
}
I get a 403 and a body parsing error:
{"message": "Forbidden", "error": 403}
Failed to comment on Reddit post 1cxbmk6: TypeError: Body is unusable
Needed to use thing_id: post.name,
instead of postId
. It includes the "type" ie: t3_$id