【问题标题】:Reddit api Oauth: Server returned HTTP response code: 411 for URL errorReddit api Oauth:服务器返回 HTTP 响应代码:411 表示 URL 错误
【发布时间】:2019-02-01 09:50:28
【问题描述】:

我正在尝试从https://www.reddit.com/api/v1/access_token 获取访问令牌。为此,我需要向上述 URL 发送 CLIENT_IDCLIENT_SECRET。我是使用 Postman 完成的:

正如屏幕截图中突出显示的那样,我发送了一个 grant_type 作为 GET 参数,其值为 client_credentials 和一个 Authorization 参数,其值为 Basic heregoestheencodedkeyandid。请求类型设置为POST。它工作正常 - 我在 JSON 响应中获得了访问令牌。

但是,当我尝试通过 Java 做同样的事情时,我收到 Server returned HTTP response code: 411 错误:

public class RedditExample {
        private static String loginLink = "https://www.reddit.com/api/v1/access_token";

 public static void main(String[] args)  {
                RedditExample redditExample = new RedditExample ();

                redditExample.login();
        }

public boolean login()  {
            try  {
                URL loginURL = new URL(loginLink + "?grant_type=client_credentials");
                HttpURLConnection connection = (HttpURLConnection) loginURL.openConnection();
                setupPOSTConnection(connection);

                InputStream input = connection.getInputStream();
                String inputString = new Scanner(input, "UTF-8").useDelimiter("\\Z").next();

                System.out.println(inputString);
            }

            catch (Exception e) {
                System.out.println(e.toString());
            }

            return true;
        }

private static void setupPOSTConnection(HttpURLConnection connection) throws Exception  {
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Authorization", "Basic heregoestheencodedkeyandid");
            connection.connect();
        }
}

与 Postman 相比,我不确定我在这里做的不同之处,因此我们将不胜感激。

编辑:这是我尝试添加的内容:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestProperty("Content-Length", "0"); connection.setRequestProperty("Content-Length", "10");

String userAgent = "test /u/someuser";
connection.setRequestProperty("User-Agent", userAgent);

不幸的是,两者都不起作用 - 错误仍然相同。

【问题讨论】:

  • 查看我编辑的答案对我有用

标签: java api http post reddit


【解决方案1】:

HttpUrlConnection 不采用显式设置内容长度。所以只需提供没有内容的请求正文。

StringBuilder postDataBuilder = new StringBuilder();
        byte[] postData = postDataBuilder.toString().getBytes("UTF-8");

        OutputStream out = conn.getOutputStream();
        out.write(postData);
        out.close();

所以方法会是这样的:-

private static void setupPOSTConnection(HttpURLConnection connection) throws Exception  {
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Authorization", "Basic heregoestheencodedkeyandid");
StringBuilder postDataBuilder = new StringBuilder();
        byte[] postData = postDataBuilder.toString().getBytes("UTF-8");

        OutputStream out = conn.getOutputStream();
        out.write(postData);
        out.close();
            connection.connect();
        }

我还发现了另一种简单地添加一行的方法:-

private static void setupPOSTConnection(HttpURLConnection connection) throws Exception  {
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");

                connection.setRequestProperty("Authorization", "Basic heregoestheencodedkeyandid");
    conn.setFixedLengthStreamingMode(0);
                connection.connect();
            }

【讨论】:

  • 非常感谢!我将永远感激不尽,我花了很多时间试图修复它!你应该得到这份赏金。
【解决方案2】:

无论出于何种原因,当服务器拒绝接受没有内容长度标头的消息时,服务器会发送 HTTP 状态代码 411(需要长度)作为响应。 服务器可能会或可能不会接受没有 Content-Length 标头的内容,而且看起来这个 API 很挑剔。请参阅 this post 详细说明使用 Java 的有效 POST 连接。

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 2014-02-22
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2016-02-24
    • 2014-08-09
    • 2013-09-12
    相关资源
    最近更新 更多