PHP code example of victorap93 / oauth2token

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

    

victorap93 / oauth2token example snippets




use Dotenv\Dotenv;
use victorap93\OAuth2Token\Microsoft;

orization code
if (!Microsoft\Authorize::getCode()) {
    $authorize = new Microsoft\Authorize($_ENV['TENANT_ID'], $_ENV['CLIENT_ID']);
    $refresh_token = $authorize->redirect(['IMAP.AccessAsUser.All', 'offline_access'], 'http://localhost');
} else {
    $grant_type = Microsoft\GrantType::authorization_code(Microsoft\Authorize::getCode(), 'http://localhost');
    $client = new Microsoft\Token($_ENV['TENANT_ID'], $_ENV['CLIENT_ID'], $_ENV['CLIENT_SECRET'], $grant_type);
    $client->getAccessToken(['IMAP.AccessAsUser.All', 'offline_access']);
}



use Dotenv\Dotenv;
use victorap93\OAuth2Token\Microsoft;

word
$grant_type = Microsoft\GrantType::password($_ENV['USERNAME'], $_ENV['PASSWORD']);
$client = new Microsoft\Client($_ENV['TENANT_ID'], $_ENV['CLIENT_ID'], $_ENV['CLIENT_SECRET'], $grant_type);
$token = $client->getToken(['IMAP.AccessAsUser.All', 'offline_access']);

$grant_type = Microsoft\GrantType::refresh_token($token->refresh_token);
$client = new Microsoft\Client($_ENV['TENANT_ID'], $_ENV['CLIENT_ID'], $_ENV['CLIENT_SECRET'], $grant_type);
$client->getToken(['IMAP.AccessAsUser.All', 'offline_access']);