PHP code example of geekdevs / oauth2-cronofy

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

    

geekdevs / oauth2-cronofy example snippets


session_start();

$provider = new Geekdevs\OAuth2\Client\Provider\Cronofy([
    'clientId'          => '{cronofy-app-id}',
    'clientSecret'      => '{cronofy-app-secret}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl([
        'scope' => ['read_account', '...', '...'],
    ]);
    $_SESSION['oauth2state'] = $provider->getState();
    
    echo '<a href="'.$authUrl.'">Log in with Cronofy!</a>';
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    echo 'Invalid state.';
    exit;

}

// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
    'code' => $_GET['code']
]);

// Optional: Now you have a token you can look up a users profile data
try {

    // We got an access token, let's now get the account's details
    $account = $provider->getResourceOwner($token);

    // Use these details to create a new profile
    printf('Hello %s!', $account->getName());
    
    echo '<pre>';
    var_dump($account);
    # object(League\OAuth2\Client\Provider\CronofyAccount)#10 (1) { ...
    echo '</pre>';

} catch (Exception $e) {

    // Failed to get account details
    exit('Oh dear...');
}

echo '<pre>';
// Use this to interact with an API on the users behalf
var_dump($token->getToken());
# string(217) "CAADAppfn3msBAI7tZBLWg...

// Number of seconds until the access token will expire, and need refreshing
var_dump($token->getExpires());
# int(1436825866)
echo '</pre>';

$account = $provider->getResourceOwner($token);

$id = $account->getId();
var_dump($id);
# string(1) "acc_567236000909002"

$name = $account->getName();
var_dump($name);
# string(15) "Pavel Dubinin"

$email = $account->getEmail();
var_dump($email);
# string(15) "[email protected]"

$timezone = $account->getDefaultTimezone();
var_dump($timezone);
# string(15) "Europe/London"

$accountData = $account->toArray();