Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't post comment on reddit using Postman: USER_REQUIRED error

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?

like image 304
parsecer Avatar asked Aug 28 '18 00:08

parsecer


1 Answers

From the information available I'd guess that you didn't provide user credentials in the token request but requested an Application Only Token instead. This type of token can probably not be used to post comments on reddit for obvious reasons.

To perform actions for a certain user you will need to request an access token like this:

reddit@reddit-VirtualBox:~$ curl -X POST -d 'grant_type=password&username=reddit_bot&password=snoo' --user 'p-jcoLKBynTLew:gko_LXELoV07ZBNUXrvWZfzE3aI' https://www.reddit.com/api/v1/access_token
{
    "access_token": "J1qK1c18UUGJFAzz9xnH56584l4", 
    "expires_in": 3600, 
    "scope": "*", 
    "token_type": "bearer"
}

That is provide user credentials as form data and use the app credentials for Basic authentication.

In general it's probably simpler to use one of the API-Wrappers that are already available for reddit. For Java the reddit wiki mentions JRAW and RedditJerk.

like image 96
dpr Avatar answered Oct 18 '22 20:10

dpr