PHP code example of bcartfall / autodesk-core

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

    

bcartfall / autodesk-core example snippets






esk\Auth\Configuration::getDefaultConfiguration()
    ->setClientId('XXXXXX')
    ->setClientSecret('XXXXXX');

$twoLeggedAuth = new Autodesk\Auth\OAuth2\TwoLeggedAuth();
$twoLeggedAuth->setScopes(['bucket:read']);

/**
 * Other options to manage the scopes
 *
 * $twoLeggedAuth->addScope('data:read');
 * $twoLeggedAuth->addScopes([]);
 * $twoLeggedAuth->setScopes($scopes);
 */

if (isset($cache['applicationToken']) && $cache['expiry'] > time()) {
    $twoLeggedAuth->setAccessToken($cache['applicationToken']);
} else {
    $twoLeggedAuth->fetchToken();

    $cache['applicationToken'] = $twoLeggedAuth->getAccessToken();
    $cache['expiry'] = time() + $twoLeggedAuth->getExpiresIn();
}




on_start();

Autodesk\Auth\Configuration::getDefaultConfiguration()
    ->setClientId('XXXXXX')
    ->setClientSecret('XXXXXX')
    ->setRedirectUrl("http://{$_SERVER['HTTP_HOST']}/callback.php");

$threeLeggedAuth = new Autodesk\Auth\OAuth2\ThreeLeggedAuth();
$threeLeggedAuth->addScope('code:all');

if (isset($_SESSION['isAuthenticated']) && $_SESSION['expiry'] > time()) {
    $threeLeggedAuth->setAccessToken($_SESSION['accessToken']);

    print_r('Token was fetched from the session');
} else {
    if (isset($_SESSION['refreshToken'])) {
        $threeLeggedAuth->refreshToken($_SESSION['refreshToken']);

        $_SESSION['isAuthenticated'] = true;
        $_SESSION['accessToken'] = $threeLeggedAuth->getAccessToken();
        $_SESSION['refreshToken'] = $threeLeggedAuth->getRefreshToken();
        $_SESSION['expiry'] = time() + $threeLeggedAuth->getExpiresIn();

        print_r('Token was refreshed');
    } else {
        $redirectTo = $threeLeggedAuth->createAuthUrl();

        header('Location: ' . filter_var($redirectTo, FILTER_SANITIZE_URL));
        return;
    }
}





on_start();

Autodesk\Auth\Configuration::getDefaultConfiguration()
    ->setClientId('XXXXXX')
    ->setClientSecret('XXXXXX')
    ->setRedirectUrl("http://{$_SERVER['HTTP_HOST']}/callback.php");

$threeLeggedAuth = new Autodesk\Auth\OAuth2\ThreeLeggedAuth();
$threeLeggedAuth->addScopes(['data:read']);

if (isset($_GET['code']) && $_GET['code']) {
    $threeLeggedAuth->fetchToken($_GET['code']);

    $_SESSION['isAuthenticated'] = true;
    $_SESSION['accessToken'] = $threeLeggedAuth->getAccessToken();
    $_SESSION['refreshToken'] = $threeLeggedAuth->getRefreshToken();
    $_SESSION['expiry'] = time() + $threeLeggedAuth->getExpiresIn();

    $url = 'http://' . $_SERVER['HTTP_HOST'] . '/';
    header('Location: ' . filter_var($url, FILTER_SANITIZE_URL));
} else {
    header('Location: ' . $threeLeggedAuth->createAuthUrl());
}