The message in Moodle doesn't include enough information to help troubleshoot these kinds of errors. You can modify the file lib/oauthlib.php as follows changing lines 570-572 from:
if ($this->info['http_code'] !== 200) {
throw new moodle_exception('Could not upgrade oauth token');
}
to:
if ($this->info['http_code'] !== 200) {
if ($this->error) {
$error = $this->error;
} else if ($this->info['http_code']) {
$error = "HTTP status: {$this->info['http_code']}";
}
throw new moodle_exception('Could not upgrade token', 'core_error', '', $response, $error);
}
Then enable debugging (Site administration ▸ Development ▸ Debugging, Debug messages: DEVELOPER and Display debug messages: Yes). Hopefully you will see more detail including the error the Microsoft OAuth 2 service is sending.