0

Im trying to refresh an Access token in my application following this solution.

My actual problem is handling the callback and then return the new request in the authenticate method.

I tried using an interface to return a String from my callback method but then I cant assign it to a variable, nor can I return the new request from there since its inside my onResponseListener.

How can I solve this issue?

    public Request authenticate(Route route, Response response) throws IOException {
        // GetAuthRequest is a void method, and I cant assign a String value on the callback.
        getAuthRequest(new AuthResponse() {
            @Override
            public Request onSuccess(String token) {
                return response.request().newBuilder()
                        .header("Authorization", "Bearer " + token)
                        .build();
            }
        });

1 Answers1

0

I was using an Asynchronous call instead of Synchronous. Ended up making a method that returns an String like so:

private String getAuthRequest() {
        // Make the request above
        try (Response response = httpClient.newCall(request).execute()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return jsonObject.getString("access_token");
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return null;
    }