WordPress PHP OAUTH2 Access token not showing credentials popup-ThrowExceptions

Exception or error:

I have a wordpress website where I use this request to get an access token. When I am logged in and I make the request I get the access token.

When I am logged out however, the browser asks for username and password. This is fine when I use the browser for this but what if I want to call this script from my code?

Normally I would get an array with the access token value but now I get the html code for logging into the system. Is there a way to display this username & password popup instead of viewing the html?

$url = 'http://***/oauth/authorize?response_type=code&client_id=***&redirect_uri=https://***redirect.php';
$options = array(
    'http' => array(
        'method'  => 'POST',
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *