-1
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
jonrsharpe
123k31 gold badges278 silver badges489 bronze badges
asked Jun 29, 2024 at 11:56

1 Answer 1

0

Needed to use thing_id: post.name, instead of postId. It includes the "type" ie: t3_$id

DarkBee
14.4k9 gold badges86 silver badges135 bronze badges
answered Jul 2, 2024 at 6:06
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.