protected Token requestToken(Request request) throws IOException {
        HttpResponse response = safeExecute(env.sslResourceHost, request.buildRequest(HttpPost.class));
        final int status = response.getStatusLine().getStatusCode();

        String error = null;
        try {
            if (status == HttpStatus.SC_OK) {
                final Token token = new Token(Http.getJSON(response));
                if (listener != null) listener.onTokenRefreshed(token);
                return token;
            } else {
                error = Http.getJSON(response).getString("error");
            }
        } catch (IOException ignored) {
            error = ignored.getMessage();
        } catch (JSONException ignored) {
            error = ignored.getMessage();
        }
        throw status == HttpStatus.SC_UNAUTHORIZED ?
                new InvalidTokenException(status, error) :
                new ApiResponseException(response, error);
    }