par*_*cer 8 java api rest post http
我正试图用Postman发表评论.我发送以下信息:
头:
Authorization: "Bearer access_token"
Content-Type: " application/x-www-form-urlencoded"
User-Agent: "some u/user"
Run Code Online (Sandbox Code Playgroud)
身体:
api_type: "json"
thing_id: "t3_9e04eo"
text: "some comment"
Run Code Online (Sandbox Code Playgroud)
我将此POST请求发送到https://oauth.reddit.com/api/comment.
作为回报,我得到一个USER_REQUIRED error:
{
"json": {
"errors": [
[
"USER_REQUIRED",
"Please log in to do that.",
null
]
]
}
}
Run Code Online (Sandbox Code Playgroud)
这是为什么?我已经通过了access_token,它被接受为正确(否则,如果我故意传递错误的令牌,我将收到401 Unauthorized错误).
我有什么密码:
我常用的用户名:密码对
我的脚本的app_id:app_secret对
我的access_token是用来换取app_id:app_secret对的.
我也尝试用Java做这个,使用HttpURLConnection类:
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);
}
}
}
?
Run Code Online (Sandbox Code Playgroud)
但我仍然得到错误输出:
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}
Run Code Online (Sandbox Code Playgroud)
还有什么我需要添加到请求以摆脱错误?
根据可用信息,我猜测您没有在令牌请求中提供用户凭据,而是请求了仅应用程序令牌。由于显而易见的原因,这种类型的令牌可能无法用于在 reddit 上发表评论。
要为特定用户执行操作,您需要请求访问令牌,如下所示:
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"
}
Run Code Online (Sandbox Code Playgroud)
即提供用户凭据作为表单数据,并使用应用程序凭据进行基本身份验证。
一般来说,使用Reddit 已有的API 包装器之一可能更简单。对于 Java,reddit wiki 提到了JRAW和RedditJerk。