【问题标题】:Can't post comment on reddit using Postman: USER_REQUIRED error无法使用 Postman 在 reddit 上发表评论:USER_REQUIRED 错误
【发布时间】:2019-02-02 12:47:36
【问题描述】:

我正在尝试使用 Postman 发表评论。我正在发送以下信息:

标题:

Authorization: "Bearer access_token"

Content-Type: " application/x-www-form-urlencoded"

User-Agent: "some u/user"

主体:

api_type: "json"

thing_id: "t3_9e04eo"

text: "some comment"

我正在将此 POST 请求发送到 https://oauth.reddit.com/api/comment

作为回报,我得到一个USER_REQUIRED error

{
    "json": {
        "errors": [
            [
                "USER_REQUIRED",
                "Please log in to do that.",
                null
            ]
        ]
    }
}

这是为什么呢?我已经传递了一个 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);
            }

       }
}
​

但我仍然得到错误输出:

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}

我还需要在请求中添加什么以消除错误?

【问题讨论】:

  • 你确定requestProperty是怎么设置header的?此外,您在Authorization 中有一个杂散空间。
  • @parsecer 你在传递 Content-Length 吗?
  • 我也想知道,如果通过邮递员的电话有效,那么这也应该有效
  • 你读过OAuth2 quick start pages吗?在 curl 示例中,对访问令牌的请求包含 "grant_type=password&username=reddit_bot&password=snoo" 作为 application/x-www-form-urlencoded 正文数据以及应用程序 ID 和应用程序客户端密码。您是否相应地请求访问令牌?
  • 如果您想回答这个问题,请添加有关访问令牌请求的信息。

标签: java rest api http post


【解决方案1】:

根据可用信息,我猜您没有在令牌请求中提供用户凭据,而是请求了Application Only Token。由于显而易见的原因,这种类型的令牌可能不能用于在 reddit 上发布 cmets。

要为某个用户执行操作,您需要像这样请求访问令牌:

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"
}

即提供用户凭据作为表单数据,并使用应用凭据进行基本身份验证。

一般来说,使用 reddit 已经可用的API-Wrappers 之一可能更简单。对于 Java,reddit wiki 提到了 JRAWRedditJerk

【讨论】:

  • 哇,你是魔术师!我现在已经失去了希望!谢谢!你是对的。我的错误在于获取了错误的 access_token:我得到了一个使用 grant_type=client_credentials 并且只包含 APP_ID:APP_SECRET 作为授权的,但我还必须在请求的 URL 中包含 usernamepassword!现在我有了正确的 access_token 并且发表评论就像一个魅力!非常感谢!
  • 我知道包装器,但由于我是整个 API 的新手,所以我想自己做所有事情以获得更好的理解。一旦我弄清楚了所有最必要的操作的正确请求,我希望为其他想要使用 Java 做同样事情的人写一个指南。
  • 抱歉,我是赏金新手,这个似乎已经过期了。万一没有得到奖励,我试图弥补它。
  • @parsecer,谢谢,没有回答赏金问题(仅 :-))。希望投票不会因为serial upvoting detection而被推翻。
猜你喜欢
  • 2013-06-25
  • 2017-12-06
  • 2020-10-22
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
相关资源
最近更新 更多