I'm trying to get an access token from https://www.reddit.com/api/v1/access_token. To do so I need to send a CLIENT_ID
and a CLIENT_SECRET
to the above URL. I did so using Postman:
As highlighted on the screenshot, I've sent a grant_type
as a GET
parameter with value client_credentials
and an Authorization
parameter with value Basic heregoestheencodedkeyandid
. The reuest type was set as POST
. It worked correctly - I got an access token in a JSON
response.
However, when I try to do the same thing by means of Java, I receieve a Server returned HTTP response code: 411
error:
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();
}
}
I'm not sure what I'm doing different here, compared to Postman, so any help would be appreciated.
EDIT: Here is what I tried adding:
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);
Unfortunately, neither worked - the error is still the same.
Setting content-length explicitly is not taken by HttpUrlConnection. So just provide request body with no content.
StringBuilder postDataBuilder = new StringBuilder();
byte[] postData = postDataBuilder.toString().getBytes("UTF-8");
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
So the method will be like this:-
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();
}
also i found another way of simply adding one line:-
private static void setupPOSTConnection(HttpURLConnection connection) throws Exception {
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic heregoestheencodedkeyandid");
conn.setFixedLengthStreamingMode(0);
connection.connect();
}