Parent class:
<?php
declare(strict_types=1);
namespace App\Commons\MicrosoftGraph;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext;
use Microsoft\Kiota\Authentication\Oauth\OnBehalfOfContext;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAccessTokenProvider;
class MicrosoftGraph
{
private GraphServiceClient $graphServiceClient;
private AuthorizationCodeContext|OnBehalfOfContext|ClientCredentialContext $context;
private array $defaultScopes = [];
public function __construct(
AuthorizationCodeContext|OnBehalfOfContext|ClientCredentialContext $context,
array $scopes = []
) {
$this->graphServiceClient = new GraphServiceClient(
$context,
$context instanceof ClientCredentialContext ? [] : [...$this->defaultScopes, ...$scopes]
);
$this->context = $context;
}
public function getGraphServiceClient(): GraphServiceClient
{
return $this->graphServiceClient;
}
public function getToken(): string
{
$tokenProvider = new GraphPhpLeagueAccessTokenProvider($this->context);
$token = $tokenProvider->getAuthorizationTokenAsync('https://graph.microsoft.com')->wait();
if ($token === null) {
throw new \RuntimeException('Unable to retrieve token');
}
return $token;
}
}
Sub class:
<?php
declare(strict_types=1);
namespace App\Commons\MicrosoftGraph;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\Config;
use League\Flysystem\FileAttributes;
use Microsoft\Kiota\Abstractions\ApiException;
class OneDriveFilesystem extends MicrosoftGraph implements FilesystemAdapter
{
private ClientCredentialContext $tokenContext;
public function __construct(string $tenantId, string $clientId, string $clientSecret)
{
$this->tokenContext = new ClientCredentialContext(
$tenantId, // common
$clientId, // my client id
$clientSecret, // my client secret
);
parent::__construct($this->tokenContext);
}
public function fileExists(string $path): bool
{
try {
$data = $this->getGraphServiceClient()->users()->byUserId('user-id')->get()->wait();
dd('data', $data);
} catch (ApiException $th) {
dd('error', $th->getError()->getMessage(), $this->getToken());
}
return true;
}
// more...
}
Laravel ServiceProvider:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Storage;
use App\Commons\MicrosoftGraph\OneDriveFilesystem;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\Filesystem;
class OneDriveServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
//
}
/**
* Bootstrap services.
*/
public function boot(): void
{
Storage::extend('onedrive', function ($app, $config) {
$adapter = new OneDriveFilesystem(
$config['tenant_id'],
$config['client_id'],
$config['client_secret'],
);
return new FilesystemAdapter(
new Filesystem($adapter, $config),
$adapter,
$config
);
});
}
}
Use:
dd(Storage::disk('onedrive')->fileExists('test'));
Get:
The identity of the calling application could not be established.