1. Go to this page and download the library: Download previewtechs/oauth2-client 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/ */
previewtechs / oauth2-client example snippets
$provider = new \Previewtechs\Oauth2\Client\Provider([
'clientId' => '{previewtechs_client_id}', // The client ID assigned to you by Preview Technologies
'clientSecret' => '{previewtechs_client_secret}', // The client secret assigned to you by Preview Technologies
'redirectUri' => '{your_redirect_url}'
]);
$provider = new \Previewtechs\Oauth2\Client\Provider([
'clientId' => '{previewtechs_client_id}', // The client ID assigned to you by Preview Technologies
'clientSecret' => '{previewtechs_client_secret}', // The client secret assigned to you by Preview Technologies
'redirectUri' => '{your_redirect_url}'
]);
$existingAccessToken = getAccessTokenFromYourDataStore();
if ($existingAccessToken->hasExpired()) {
$newAccessToken = $provider->getAccessToken('refresh_token', [
'refresh_token' => $existingAccessToken->getRefreshToken()
]);
//Remove old and save new access token in your database
}
ion_start();
$provider = new \Previewtechs\Oauth2\Client\Provider([
'clientId' => '{previewtechs_client_id}',
// The client ID assigned to you by Preview Technologies
'clientSecret' => '{previewtechs_client_secret}',
// The client password assigned to you by Preview Technologies
'redirectUri' => '{your_redirect_url}'
]);
// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {
// Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters
// (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl();
// Get the state generated for you and store it to the session.
$_SESSION['oauth2state'] = $provider->getState();
// Redirect the user to the 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 $accessToken->getToken() . "\n";
echo $accessToken->getRefreshToken() . "\n";
echo $accessToken->getExpires() . "\n";
echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n";
// Using the access token, we may look up details about the
// resource owner.
$resourceOwner = $provider->getResourceOwner($accessToken);
var_export($resourceOwner->toArray());
// We provides a way to get an authenticated API request for
// the service, using the access token; it returns an object conforming
// to Psr\Http\Message\RequestInterface.
$request = $provider->getAuthenticatedRequest(
'GET',
'https://myaccount.previewtechs.com/api/v1/identity/user-info',
$accessToken
);
$client = new \GuzzleHttp\Client();
$result = $client->send($request);
var_export(json_decode($result->getBody()->getContents(), true));
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token or user details.
exit($e->getMessage());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.