Add baerer token in post request

Hi everyone, in a post request made through F.Promise , I add a baerer token, but I get this error (via postman the request works correctly):

{"ErrorCode":"401","ErrorDescription":"Authorization error (invalid_token): Invalid access token","ErrorName":"Unauthorized","ErrorStack":null}

The code is ( see the row about .setHeader(“Authorization”,"Bearer "+accessToken)):

        F.Promise<WSResponse> responsePromise = WS.url("https://XXXXXXXXXXXXXX/Authorization/OAuth/Token")
                .setHeader("Content-Length", String.valueOf(body.length()))
                .setHeader("Authorization", "Basic" + "Basic " + auth )
                .post(body).map(wsResponse -> {
          "Token received from MailUp -  status call : ", wsResponse.getStatus() );
                    JsonNode node = Json.parse(wsResponse.getBody());
                    if( wsResponse != null && wsResponse.getStatus() == 200){
                        JsonNode accessToken = node.path("access_token");
              "access node get correctly : "+ accessToken);

                                .setHeader("Authorization","Bearer "+accessToken)
                                .post("{\n" + \"Subject\":\"Appointment #1\",\n"
                                        "}").map(wsResponseSMS -> {
                                  " cretion  result "+wsResponseSMS.getBody());
                                            return wsResponseSMS;


How could I map correctly the baerer token? Thanks

Your accessToken is still a JsonNode. So when you print that out, you will get extra " on both sides. To convert a JsonNode of a text to String use the method asText. So your line should look like

String accessToken = node.path("access_token").asText();

1 Like