PHP code example of liftkeeper / oauth2-tsheets

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

    

liftkeeper / oauth2-tsheets example snippets





n_start();

$provider = new \Liftkeeper\OAuth2\Client\Provider\TSheets([
	'clientId' => '{__TSHEETS-CLIENT-ID__}',
	'clientSecret' => '{__TSHEETS-CLIENT-SECRET__}',
	'redirectUri' => '{__YOUR-CALLBACK-URL__}',
]);

if (!isset($_GET['code'])) {
	$authUrl = $provider->getAuthorizationUrl();
	$_SESSION['oauth2state'] = $provider->getState();
	header('Location: ' . $authUrl);
	exit;

} else if (empty($_GET['state']) || ( $_GET['state'] !== $_SESSION['oauth2state'] )) {
	unset($_SESSION['oauth2state']);
	exit('Invalid state');

} else {
	try {
		$accessToken = $provider->getAccessToken('authorization_code', [
			'code' => $_GET['code'],
		]);

		echo '<pre>';
		echo 'Token: ' . $accessToken->getToken() . PHP_EOL;
		echo 'RefreshToken: ' . $accessToken->getRefreshToken() . PHP_EOL;
		echo 'Expires: ' . $accessToken->getExpires() . PHP_EOL;
		echo 'hasExpired: ' . ( $accessToken->hasExpired() ? 'expired' : 'not expired' ) . PHP_EOL;

		$resourceOwner = $provider->getResourceOwner($accessToken);

		print_r($resourceOwner);
		echo '</pre>';

	} catch (Exception $e) {
		exit($e->getMessage());

	} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
		exit($e->getMessage());
	}
}





$provider = new \Liftkeeper\OAuth2\Client\Provider\TSheets([
	'clientId' => '{__TSHEETS-CLIENT-ID__}',
	'clientSecret' => '{__TSHEETS-CLIENT-SECRET__}',
	'redirectUri' => '{__YOUR-CALLBACK-URL__}',
]);

// load stored accessToken
// $accessToken = loadAccessToken();

$newAccessToken = $provider->getAccessToken('refresh_token', [
	'grant_type' => 'refresh_token',
	'access_token' => $accessToken
]);

// save new accessToken
// saveAccessToken($newAccessToken);