Home > Enterprise >  How do I download TheTVDB JSON data using Java
How do I download TheTVDB JSON data using Java

Time:12-05

I have the appropriate API Key and Subscriber Pin but I'm not sure how to create the command line to login or to pull actual data from TheTVDB database. Does anyone have an example of how to do that?

This is the code that I am using but it gives me a 405 response code.

URL url = new URL("https://api4.thetvdb.com/v4/login?apikey=xxxxxxxx&pin=XXXXXXXX");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("accept", "application/json");
    conn.setRequestMethod("GET");
    conn.connect();

    //Getting the response code
    int responsecode = conn.getResponseCode();
    System.out.println("ResponseCode: "   responsecode);

    if (responsecode != 200) {
        throw new RuntimeException("HttpResponseCode: "   responsecode);
    } else {
        String inline = "";

        Scanner scanner = new Scanner(url.openStream());

        //Write all the JSON data into a string using a scanner
        while (scanner.hasNext()) {
            inline  = scanner.nextLine();
        }
        System.out.println("inline: "   inline);
    }

CodePudding user response:

This part is mostly correct:

URL url = new URL("https://api4.thetvdb.com/v4/login?apikey=xxxxxxxx&pin=XXXXXXXX");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("accept", "application/json");
conn.setRequestMethod("GET");
conn.connect();

//Getting the response code
int responsecode = conn.getResponseCode();
System.out.println("ResponseCode: "   responsecode);

if (responsecode != 200) {
    throw new RuntimeException("HttpResponseCode: "   responsecode);

…except that, according to the documentation, you need to pass the key and PIN as JSON values in the request body, not as URL query parameters. The documentation has this example request body:

{
  "apikey": "string",
  "pin": "string"
}

So, remove those from your URL:

URL url = new URL("https://api4.thetvdb.com/v4/login");

And send them as a JSON object. (I’m using Java EE’s javax.json package, but there are several other JSON libraries that will work.)

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");

// According to the documentation, /login is a POST call.
conn.setRequestMethod("POST");
conn.setDoOutput(true);

JsonObjectBuilder builder = Json.createObjectBuilder();
JsonObject requestBody =
    builder.add("apikey", myAPIKey).add("pin", myPIN).build();

try (JsonWriter writer = new Json.createWriter(conn.getOutputStream())) {
    writer.writeObject(requestBody);
}

Similarly, you must parse the response body as JSON. The documentation has this example response body:

{
  "data": {
    "token": "string"
  },
  "status": "string"
}

So you want to parse it and read the data/token attribute:

//Getting the response code
int responsecode = conn.getResponseCode();
System.out.println("ResponseCode: "   responsecode);

if (responsecode != 200) {
    throw new RuntimeException("HttpResponseCode: "   responsecode);
}

JsonObject responseBody;
try (JsonReader reader = Json.createReader(conn.getInputStream())) {
    responseBody = reader.readObject();
}

String token = responseBody.getJsonObject("data").getString("token");

Now you can use the token for future requests, as demonstrated at the top of the documentation page:

url = new URL("https://api4.thetvdb.com/v4/movies?page=1");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer "   token);
conn.setRequestProperty("Accept", "application/json");

responsecode = conn.getResponseCode();
if (responsecode != 200) {
    throw new RuntimeException("HttpResponseCode: "   responsecode);
}

try (JsonReader reader = Json.createReader(conn.getInputStream())) {
    responseBody = reader.readObject();
}

JsonArray movieEntries = responseBody.getJsonArray("data");
int count = movieEntries.size();
for (int i = 0; i < count; i  ) {
    JsonObject movieEntry = movieEntries.getJsonObject(i);
    String movieName = movieEntry.getString("name");

    System.out.println("Found movie \""   movieName   "\"");
}

The specific JSON attribute names, like "data" and "name", are all listed in the documentation page. Click on any URL path in a colored box on that page to expand it and see all of the details.

  • Related