Я пытаюсь оставить комментарий с помощью Почтальона. Я отправляю следующую информацию:
Заголовки:
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"
Я отправляю этот запрос POST на https://oauth.reddit.com/api/comment.
В ответ я получаю USER_REQUIRED error
:
1019 *
{
"json": {
"errors": [
[
"USER_REQUIRED",
"Please log in to do that.",
null
]
]
}
}
Почему это? Я передал access_token, и он был принят как правильный (в противном случае, если я сознательно передам неправильный токен, я получу 401 Несанкционированную ошибку).
Какие у меня пароли:
Мое обычное имя пользователя: пара паролей
app_id моего скрипта: пара app_secret
Мой access_token мне дали в обмен на мою пару app_id: app_secret.
Я также пытался сделать это на Java, используя 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);
}
}
}
Но я все еще получаю сообщение об ошибке:
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}
Что еще мне нужно добавить в запрос, чтобы избавиться от ошибки?