I'm trying to post a comment using Postman. I'm sending the following information:
Headers:
Authorization: "Bearer access_token"
Content-Type: " application/x-www-form-urlencoded"
User-Agent: "some u/user"
Body:
api_type: "json"
thing_id: "t3_9e04eo"
text: "some comment"
I'm sending this POST request to https://oauth.reddit.com/api/comment.
In return I get a USER_REQUIRED error:
ā
{
"json": {
"errors": [
[
"USER_REQUIRED",
"Please log in to do that.",
null
]
]
}
}
Why is that? I've passed an access_token and it was accepted as right (otherwise if I knowingly pass the wrong token I would get a 401 Unauthorized error).
What I have of the passwords:
My usual username:password pair
My script's app_id:app_secret pair
My access_token I was given in exchange for my app_id:app_secret pair.
I also tried to do this in Java, using HttpURLConnection class:
import org.apache.tomcat.util.codec.binary.Base64;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class RedditParser {
public static void main(String[] args) {
RedditParser redditParser = new RedditParser();
redditParser.postAComment("sds", "fdfdf");
}
public void postAComment(String postID, String commentBody) {
try {
String postLink = "https://oauth.reddit.com/api/comment";
URL loginURL = new URL(postLink);
HttpURLConnection connection = (HttpURLConnection) loginURL.openConnection();
JSONObject requestJSON = new JSONObject();
requestJSON.put("api_type", "json");
requestJSON.put("thing_id", "t3_9e04eo");
requestJSON.put("text", "a test comment");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Bearer " +getAccessToken()); //getAccessToken returns correct(!) token; it's not the cause of the error
connection.setRequestProperty("User-Agent", "script by /u/someuser");
connection.setRequestProperty("Content-Type", "application/json");
OutputStream os = connection.getOutputStream();
os.write(requestJSON.toString().getBytes("UTF-8"));
os.close();
connection.connect();
System.out.println("Done comment");
InputStream input = connection.getInputStream();
String inputString = new Scanner(input, "UTF-8").useDelimiter("\\Z").next();
JSONObject jsonObject = new JSONObject(inputString);
System.out.println(inputString);
}
catch (Exception e) {
System.out.println(e);
}
}
}
ā
But I still get the error output:
Done comment
{"jquery": [[0, 1, "refresh", []], [0, 2, "attr", "find"], [2, 3, "call", [".error.USER_REQUIRED"]], [3, 4, "attr", "show"], [4, 5, "call", []], [5, 6, "attr", "text"], [6, 7, "call", ["Please log in to do that."]], [7, 8, "attr", "end"], [8, 9, "call", []]], "success": false}
What else do I need to add to the request to get rid of the error?