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');

$baseurl="https://api.tradegecko.com/oauth/token";
$auth_code="";
$client_id="MYCLIENTID";
$client_secret="MYSECRET";
$redirect_uri="http://localhost:81/postman/public/request/instantadd";

if ($token_storage->hasToken() === false) {
    $auth_url = 'https://api.tradegecko.com/oauth/authorize?'.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);
$oauth->setTokenPersistence($token_storage);
$stack = HandlerStack::create();
$stack->push($oauth);

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

$response = $client->get($requesturl, $options);
$result=json_decode($response->getBody(),true);

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.

Regards,
Rekha

How to solve:

Leave a Reply

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