Reddit api Oauth:服务器返回 HTTP 响应代码:411 URL 错误

Reddit api Oauth: Server returned HTTP response code: 411 for URL error

我正在尝试从 https://www.reddit.com/api/v1/access_token 获取访问令牌。为此,我需要向上面的 URL 发送一个 CLIENT_ID 和一个 CLIENT_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);

遗憾的是,两者均无效 - 错误仍然相同。

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

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();
            }