This package contains a Flysystem adapter for OneDrive. Under the hood, the Microsoft Graph SDK is used.
This package requires PHP 8.1+ and Flysystem v3.
You can install the package using composer:
composer require leapt/flysystem-onedrive
The first thing you need to do is get an authorization token for the Microsoft Graph API. For that you need to create an app on the Microsoft Azure Portal.
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');
If you are looking for a way to retrieve a bearer token, here are two examples:
- first example using Symfony HTTP Client
- second example using Guzzle, which is already included as a dependency from Microsoft Graph SDK
$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'];
Please see CHANGELOG for more information what has changed recently.
Feel free to contribute, like sending pull requests to add features/tests or creating issues :)
Note there are a few helpers to maintain code quality, that you can run using these commands:
composer cs:dry # Code style check
composer phpstan # Static analysis
vendor/bin/phpunit # Run tests
The MIT License (MIT). Please see License File for more information.
This bundle is a maintained fork of the packages nicolasbeauvais and hevelius.