0

i need to convert the following curl command into java command.

curl https://api.linkedin.com/v2/me -H "Authorization: Bearer xxx"

I writed this code:

public static void main(String[] args) throws MalformedURLException, IOException{

    HttpURLConnection con = (HttpURLConnection) new URL("https://api.linkedin.com/v2/me").openConnection();

    con.setRequestMethod("POST");
    con.setRequestProperty("Authorization", "Bearer xxx");
    con.setDoOutput(true);
    con.setDoInput(true);
    con.getOutputStream().write("LOGIN".getBytes("UTF-8"));
    con.getInputStream();
}

but I get an error:

Exception in thread "main" java.io.FileNotFoundException: https://api.linkedin.com/v2/me
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1915)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
    at testiamo.main(testiamo.java:18)
1
  • Try HttpsURLConnection con = (HttpsURLConnection)new URL("https://api.linkedin.com/v2/me").openConnection(); (import javax.net.ssl.HttpsURLConnection;) Commented Aug 3, 2021 at 10:51

1 Answer 1

1

I would go with Apache HttpClient

And perform the call this way:

        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpPost postRequest = new HttpPost("https://api.linkedin.com/v2/me");
        postRequest.addHeader("Authorization", "Bearer xxx");

        // You could also send something through JSON...
        if(requestBody != null)
        {
            postRequest.addHeader("content-type", "application/json");
            StringEntity jsonEntity = new StringEntity(requestBody);
            postRequest.setEntity(jsonEntity);
        }

        // Getting the response
        HttpResponse rawResponse = client.execute(postRequest);
        final int status = rawResponse.getStatusLine().getStatusCode();
        final StringBuilder response = new StringBuilder();

        // If status == 200 then we get the response (which could be JSON, XML and so on) and save it as a string.
        if(status == HttpStatus.SC_OK)
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    rawResponse.getEntity().getContent()));

            String inputLine;

            while ((inputLine = reader.readLine()) != null)
            {
                response.append(inputLine);
            }

            reader.close();
            client.close();
        }
        else
        {
            client.close();
            throw new RuntimeException("Error while doing call!\nStatus: "+ status);
        }

For more info, click here.

Sign up to request clarification or add additional context in comments.

3 Comments

I get this error: ago 04, 2021 10:30:17 AM org.apache.http.client.protocol.ResponseProcessCookies processCookies WARNING: Invalid cookie header: "Set-Cookie: li_gc=MTswOzE2MjgwNjU4MTc7MjswMjFHaojFfu7ZrccbD2V9NU7pLUJlhg+my7cV7lmf3L/aRg==; Domain=.linkedin.com; Expires=Fri, 21 Jul 2023 00:02:44 GMT; Path=/; Secure; SameSite=None". Invalid 'expires' attribute: Fri, 21 Jul 2023 00:02:44 GMT Exception in thread "main" java.lang.RuntimeException: Error while doing call! Status: 401 at testiamo.main(testiamo.java:47) Process finished with exit code
The question as has been asked above has been answered since the call is performed and you get as a response the HTTP STATUS 401. That error code means that you are unauthorized. If you need help regarding this error, I would suggest asking another specific question and put all the details in there. I will gladly help. httpstatuses.com/401
Thanks, I have just asked another question: stackoverflow.com/questions/68648697/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.