PHP code example of evert / oauth2

1. Go to this page and download the library: Download evert/oauth2 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/ */

    

evert / oauth2 example snippets




ntId = 'your client id';
$clientSecret = 'your client secret';

$redirectUri = 'http://url/of/this.php';
$authorizationEndPoint = 'https://graph.facebook.com/oauth/authorize';
$tokenEndPoint = 'https://graph.facebook.com/oauth/access_token';

$client = new OAuth2\Client($clientId, $clientSecret);
if (!isset($_GET['code']))
{
    $auth_url = $client->getAuthenticationUrl($authorizationEndPoint, $redirectUri);
    header('Location: ' . $auth_url);
    die('Redirect');
}
else
{
    $params = array('code' => $_GET['code'], 'redirect_uri' => $redirectUri);
    $response = $client->getAccessToken($tokenEndPoint, 'authorization_code', $params);
    parse_str($response['result'], $info);
    $client->setAccessToken($info['access_token']);
    $response = $client->fetch('https://graph.facebook.com/me');
    var_dump($response, $response['result']);
}



namespace OAuth2\GrantType;

/**
 * MyCustomGrantType Grant Type
 */
class MyCustomGrantType implements IGrantType
{
    /**
     * Defines the Grant Type
     *
     * @var string  Defaults to 'my_custom_grant_type'.
     */
    const GRANT_TYPE = 'my_custom_grant_type';

    /**
     * Adds a specific Handling of the parameters
     *
     * @return array of Specific parameters to be sent.
     * @param  mixed  $parameters the parameters array (passed by reference)
     */
    public function validateParameters(&$parameters)
    {
        if (!isset($parameters['first_mandatory_parameter']))
        {
            throw new \Exception('The \'first_mandatory_parameter\' parameter must be defined for the Password grant type');
        }
        elseif (!isset($parameters['second_mandatory_parameter']))
        {
            throw new \Exception('The \'seconde_mandatory_parameter\' parameter must be defined for the Password grant type');
        }
    }
}

$response = $client->getAccessToken(TOKEN_ENDPOINT, 'my_custom_grant_type', $params);