fedpep
May 24, 2019, 11:55am
1
Hello, I’m working with the TVDB API but I’m having trouble getting the access token as I normally do.
This is what the documentation says:
Authentication
Authentication to use the API is similar to the How-to section above. Users must POST to the /login route with their API key and credentials in the following format in order to obtain a JWT token.
{“apikey”:“APIKEY”,“username”:“USERNAME”,“userkey”:“USERKEY”}
And this is my code (I’m using @shiffman and @runemadsen 's library)
PostRequest post = new PostRequest("https://api.thetvdb.com/login");
post.addHeader("Content-Type", "application/json");
post.addHeader("Accept", "application/json");
post.addData("json", "{\"apikey\":\"APIKEY\",\"username\":\"USERNAME\",\"userkey\":\"USERKEY\"}");
but it’s not working.
I also tried:
post.addData("apikey", "APIKEY");
post.addData("username", "USERNAME");
post.addData("userkey", "USERKEY");
I think I have add the json to the “body” of the POST request but I can’t figure out how.
Thanks!
1 Like
fedpep
May 25, 2019, 10:26am
2
I figure out a solution:
I downloaded the jar files of the Apache HTTPComponents library (on which HTTP Requests for Processing is based on) and added to my sketch’s folder
I built an HTTP Request like this (following the examples on the website):
String authentication = "{\"apikey\":\"APIKEY\",\"username\":\"USERNAME\",\"userkey\":\"USERKEY\"}";
HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead
try {
HttpPost request = new HttpPost("https://api.thetvdb.com/login");
StringEntity params =new StringEntity(authentication);
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
JSONObject json = parseJSONObject(EntityUtils.toString(entity));
println(json.getString("token"));
//handle response here...
}catch (Exception ex) {
//handle exception here
} finally {
//Deprecated
//httpClient.getConnectionManager().shutdown();
}
and it works.
Now I’m wondering if there’s a way to do it without importing the Apache’s library or to add this feature to the Processing’s library.
3 Likes