leapt/flysystem-onedrive

Flysystem Adapter for the OneDrive API

v1.0.1 2022-08-29 12:46 UTC

This package is auto-updated.

Last update: 2024-12-09 17:01:55 UTC


README

Package version Build Status PHP Version License Code coverage

This package contains a Flysystem adapter for OneDrive. Under the hood, the Microsoft Graph SDK is used.

Installation

This package requires PHP 8.1+ and Flysystem v3.

You can install the package using composer:

composer require leapt/flysystem-onedrive

Usage

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');

Retrieve a bearer token

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

Using Symfony HTTP Client

$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'];

Using Guzzle

$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'];

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

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

License

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

History

This bundle is a maintained fork of the packages nicolasbeauvais and hevelius.