php – Laravel OAuth 2.0 Authentication using Guzzle Client-ThrowExceptions

Exception or error:

I am developing an application like Postman Client in Laravel. For that, I am using Guzzle Client.

For OAuth 2.0 authentication, I have used the below link as reference,

Reference Link

I have tried the below code,

$token_storage = new FileTokenPersistence('/tmp/token.txt');


if ($token_storage->hasToken() === false) {
    $auth_url = ''.http_build_query([
        'client_id' => $client_id,
        'redirect_uri' => $redirect_uri,
        'response_type' => 'code',
        'prompt' => 'select_account',
        'scope' => '',
        'access_type' => 'offline',

    echo "Go to the following link in your browser:\n\n";
    echo "    $auth_url\n\n";
    // if(! defined('STDIN')) define('STDIN', fopen("php://stdin","r"));
    echo "Enter verification code: ";
    $auth_code = trim(fgets(STDIN, 1024));

$reauth_client = new \GuzzleHttp\Client(['verify' => 'E:\cacert.pem',
    'base_uri' => $baseurl,

$reauth_config = [
    'code' => $auth_code,
    'client_id' => $client_id,
    'client_secret' => $client_secret,
    'redirect_uri' => $redirect_uri,

$grant_type = new AuthorizationCode($reauth_client, $reauth_config);
$refresh_grant_type = new RefreshToken($reauth_client, $reauth_config);
$oauth = new OAuth2Middleware($grant_type, $refresh_grant_type);
$stack = HandlerStack::create();

$client = new \GuzzleHttp\Client(['verify' => 'E:\cacert.pem',
    'handler' => $stack,
    'auth'    => 'oauth',

$response = $client->get($requesturl, $options);

I am getting an error in auth code retrieving. When I go to the authurl link, I am getting the token.

The error I am getting is:

"message": "Unable to request a new access token"

Also, I don’t know what is the use of STDIN…..How can I put the auth code here?

Please help me.


How to solve:

Leave a Reply

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