PHP code example of leapt / flysystem-onedrive

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

    

leapt / flysystem-onedrive example snippets


use League\Flysystem\Filesystem;
use Leapt\FlysystemOneDrive\OneDriveAdapter;
use Microsoft\Graph\Graph;

$graph = new Graph();
$graph->setAccessToken('EwBIA8l6BAAU7p9QDpi...');

$adapter = new OneDriveAdapter($graph);
$filesystem = new Filesystem($adapter);

// Or to use the approot endpoint:
$adapter = new OneDriveAdapter($graph, 'special/approot');

$tenantId = 'your tenant id';
$clientId = 'your client id';
$clientSecret = 'your client secret';
$scope = 'https://graph.microsoft.com/.default';
$oauthUrl = sprintf('https://login.microsoftonline.com/%s/oauth2/v2.0/token', $tenantId);

$client = \Symfony\Component\HttpClient\HttpClient::create();
$response = $client->request('GET', $oauthUrl, ['body' => [
    'client_id'     => $clientId,
    'scope'         => $scope,
    'grant_type'    => 'client_credentials',
    'client_secret' => $clientSecret,
]]);
$bearerToken = $response->toArray()['access_token'];

$tenantId = 'your tenant id';
$clientId = 'your client id';
$clientSecret = 'your client secret';
$scope = 'https://graph.microsoft.com/.default';
$oauthUrl = sprintf('https://login.microsoftonline.com/%s/oauth2/v2.0/token', $tenantId);

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $oauthUrl, ['form_params' => [
    'client_id'     => $clientId,
    'scope'         => $scope,
    'grant_type'    => 'client_credentials',
    'client_secret' => $clientSecret,
]]);
$bearerToken = json_decode((string) $response->getBody(), true)['access_token'];