PHP code example of klapaudius / oauth2-php

1. Go to this page and download the library: Download klapaudius/oauth2-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

klapaudius / oauth2-php example snippets


use OAuth2\OAuth2;

$oauth = new OAuth2($myStorage);

// Token endpoint — returns a HttpFoundation Response, either the token or the OAuth2 error
try {
    $response = $oauth->grantAccessToken($request);
} catch (\OAuth2\OAuth2ServerException $e) {
    $response = $e->getHttpResponse();
}

// Authorize endpoint, once the end-user has approved (or denied) the request
$response = $oauth->finishClientAuthorization($isApproved, $user, $request);

// Resource endpoint
$token = $oauth->verifyAccessToken($oauth->getBearerToken($request), 'my_scope');

use OAuth2\OAuth2;
use OAuth2\OAuthTokenGrantedEvent;

$oauth = new OAuth2($myStorage, [], $eventDispatcher);

public function onTokenGranted(OAuthTokenGrantedEvent $event): void
{
    $token = $event->getToken();
    $token['id_token'] = $this->buildIdToken(
        $event->getUser(),
        $event->getClient(),
        $event->getNonce(),      // the "nonce" sent to the authorize endpoint, replayed here
        $event->getAuthTime()    // when the end-user actually authenticated
    );
    $event->setToken($token);
}
bash
composer