elegantmedia/oauth2-myob

MYOB oAuth Client (based on League's conventions)

v0.1.0 2021-04-21 13:30 UTC

This package is auto-updated.

Last update: 2024-11-21 21:25:38 UTC


README

Latest Version on Packagist Software License

This package allows you to use Myob oAuth API.

Before using it, you'll need an API key from developer website.

Other reference links

Install

Via Composer

$ composer require elegantmedia/oauth2-myob

Usage

Authorization

The example below shows how to get an authorization URL from the Provider. For a complete example see examples/auth.php.

require __DIR__ . '/vendor/autoload.php';

$provider = new \ElegantMedia\OAuth2\Client\Provider\Myob([
	'clientId' => 'your_client_id',
	'clientSecret' => 'your_secret',
	'redirectUri' => 'https://example.com/your-url',
]);

// get initial authorization URL
$authUrl = $provider->getAuthorizationUrl();

// save the state value in session to prevent CSRF attacks
$_SESSION['oauth2state'] = $provider->getState();

header('Location: ' . $authUrl);
exit;

Get Token

The example below shows how to get an access token from the Provider. For a complete example see examples/post.php.

// valid state, so we try to get an access token
$accessToken = $provider->getAccessToken('authorization_code', [
	'code' => $params['code'],
]);

// You'll have to store these for later use
echo 'Access Token: ' . $accessToken->getToken() . "\r\n\r\n";
echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "\r\n\r\n";
echo 'Expires in: ' . $accessToken->getExpires() . "\r\n\r\n";
echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\r\n\r\n";

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

// Get resource User
// https://apisupport.myob.com/hc/en-us/articles/360003860376-Validating-the-User
echo 'User ID : ' . $user->getId() . "\r\n";
echo 'Username : ' . $user->getUsername() . "\r\n";

Refresh Token

The example below shows how to get an a new token for an expired code. For a complete example see examples/post.php.

$refreshToken = 'old-token';
$expires = 'old-timestamp';

$expiresAt = \DateTime::createFromFormat('U', $expires);

if ($expiresAt->getTimestamp() > time()) {
	echo "Not expired \r\n";
} else {
	echo "Expired \r\n";

	$accessToken = $provider->getAccessToken('refresh_token', [
		'refresh_token' => $refreshToken,
	]);

	// We have an access token, which we may use in authenticated
	// requests against the service provider's API.

	echo 'Access Token: ' . $accessToken->getToken() . "\r\n";
	echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "\r\n";
	echo 'Expires in: ' . $accessToken->getExpires() . "\r\n";
	echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\r\n";

}

$user = $provider->getResourceOwner($accessToken);
echo 'User ID : ' . $user->getId() . "\r\n";
echo 'Username : ' . $user->getUsername() . "\r\n";

Contributing

If you see any bugs, please send a pull-request.

Credits

License

The MIT License (MIT). Please see License File for more information.