I have a small problem related to retrieving the access token using REST API. What is weird is that although everything works fine in the local environment, the story changes when using the same code on the testing environment. In WordPress I have this small piece of code:

The problem here is that although on local server everything works ok, and I can retrieve the authentication token, on the testing server, this does not work, the resulting decoded json of the response being this:

A good thing to unlearn: don't provide code if the question is truly about an HTTP/JSON API.

To prove there's an actual issue with the HTTP API, take all PHP code out of the equation as that code has no actual bearing on the API's operation (and, of course, the code you've shown is a tiny portion of the code actually being run since you're using a PHP HTTP library that isn't shown, nor is the code in which the instance $this->variables are being set, nor is the configuration file from which the client ID, client secret, and base URL are being read).

Removing PHP means using a debugging proxy server, or using an interactive HTTP client like cURL or Postman. Or just dumping the JSON request/response to the screen. Anything is better than confusing a PHP problem for an API problem!

Since your results are different across two environments and you say the code is the same, then the error is surely in your environment config. You may have the $this->identityUrl set incorrectly, for example (of course you haven't provided that value, so it's impossible to know). If it's accidentally set to