0

I am getting

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 401 at jasonreader.main(jasonreader.java:21)

while executing below code, I am trying to GET data from rest API.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class jasonreader {

    // http://localhost:8080/RESTfulExample/json/product/get
    public static void main(String[] args) {

      try {

        URL url = new URL(" https://api.linkedin.com/v1/companies/1337/updates?start=20&count=10&format=json");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

      }

    }

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
seema
  • 29
  • 1
  • 7

1 Answers1

0

Doing this request with Postman, I got this body:

{
"errorCode": 0,
"message": "Unknown authentication scheme",
"requestId": "JP2QSQXZTG",
"status": 401,
"timestamp": 1469513993009
}

That means that you need access token. Please refer to this link for more info or Linkedin documentation

Community
  • 1
  • 1
Heisenberg
  • 3,153
  • 3
  • 27
  • 55