PHP code example of lustmored / oauth2-lockme

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

    

lustmored / oauth2-lockme example snippets


use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Lockme\OAuth2\Client\Provider\Lockme;

// Initialize the provider
$provider = new Lockme([
    'clientId'     => '{lockme-client-id}',
    'clientSecret' => '{lockme-client-secret}',
    'redirectUri'  => 'https://example.com/callback-url',
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Get authorization URL
    $authorizationUrl = $provider->getAuthorizationUrl([
        'scope' => 'rooms_manage' // Optional: specify scopes
    ]);

    // Get state and store it to the session
    $_SESSION['oauth2state'] = $provider->getState();

    // Redirect user to authorization URL
    header('Location: ' . $authorizationUrl);
    exit;

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

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

} else {

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

        // We have an access token, which we may use in authenticated
        // requests against the service provider's API.
        echo 'Access Token: ' . $accessToken->getToken() . "<br>";
        echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
        echo 'Expires: ' . $accessToken->getExpires() . "<br>";
        echo 'Already expired? ' . ($accessToken->hasExpired() ? 'Yes' : 'No') . "<br>";

        // Using the access token, get the user's details
        $resourceOwner = $provider->getResourceOwner($accessToken);

        // Show user ID
        var_dump($resourceOwner->getId());

        // Show all user data
        var_dump($resourceOwner->toArray());

    } catch (IdentityProviderException $e) {
        // Failed to get the access token or user details.
        exit($e->getMessage());
    }
}

$refreshToken = $accessToken->getRefreshToken();

// Verify token has expired
if ($accessToken->hasExpired()) {
    $accessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $refreshToken
    ]);
}

try {
    // Make an authenticated API request
    $response = $provider->executeRequest(
        'GET',                // HTTP method
        '/endpoint',          // API endpoint
        $accessToken,         // Access token
        ['param' => 'value']  // Optional request body (will be JSON encoded)
    );

    // Process the response
    var_dump($response);

} catch (IdentityProviderException $e) {
    // Handle error
    echo $e->getMessage();
}

$provider = new Lockme([
    'clientId'        => '{lockme-client-id}',      // Required
    'clientSecret'    => '{lockme-client-secret}',  // Required
    'redirectUri'     => 'https://example.com/',    // Required
    'apiDomain'       => 'https://api.lock.me',     // Optional: custom API domain
    'version'         => 'v2.1',                    // Optional: API version
    'ignoreSslErrors' => false                      // Optional: ignore SSL errors
]);