diff --git a/CHANGELOG.md b/CHANGELOG.md index 44dad4e5..9a75740c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.1.2] - 2024-04-21 + +### Added + +- `GraphServiceClient::withUserCredentials` and `GraphServiceClient::withClientSecret` methods introduced + +### Fixed + +- #337: Don't try to retrieve next set of items when using an iterator, if no next items are expected to exist ## [3.0.3] - 2023-07-15 diff --git a/examples/OneDrive/DownloadFiles.php b/examples/OneDrive/DownloadFiles.php index 01c726e9..2254fda8 100644 --- a/examples/OneDrive/DownloadFiles.php +++ b/examples/OneDrive/DownloadFiles.php @@ -4,22 +4,14 @@ use Office365\GraphServiceClient; use Office365\OneDrive\DriveItems\DriveItem; -use Office365\Runtime\Auth\AADTokenProvider; -use Office365\Runtime\Auth\UserCredentials; -function acquireToken() -{ - $resource = "https://graph.microsoft.com"; - $settings = include('../../tests/Settings.php'); - $provider = new AADTokenProvider($settings['TenantName']); - return $provider->acquireTokenForPassword($resource, $settings['ClientId'], - new UserCredentials($settings['UserName'], $settings['Password'])); -} - -$client = new GraphServiceClient("acquireToken"); +$settings = include('../../tests/Settings.php'); +$client = GraphServiceClient::withUserCredentials( + $settings['TenantName'], $settings['ClientId'], $settings['UserName'], $settings['Password'] +); -$items = $client->getMe()->getDrive()->getRoot()->getChildren()->get()->executeQuery(); +$items = $client->getMe()->getDrive()->getRoot()->getChildren()->top(10)->get()->executeQuery(); /** @var DriveItem $item */ foreach ($items as $item){ if($item->isFile()){ diff --git a/examples/Reports/getReports.php b/examples/Reports/getReports.php index d688f44b..623969fd 100644 --- a/examples/Reports/getReports.php +++ b/examples/Reports/getReports.php @@ -2,22 +2,12 @@ use Office365\GraphServiceClient; -use Office365\Runtime\Auth\AADTokenProvider; -use Office365\Runtime\Auth\ClientCredential; require_once '../vendor/autoload.php'; -function acquireToken() -{ - $resource = "https://graph.microsoft.com"; - $settings = include('../../tests/Settings.php'); - $provider = new AADTokenProvider($settings['TenantName']); - return $provider->acquireTokenForClientCredential($resource, - new ClientCredential($settings['ClientId'], $settings['ClientSecret']),["/.default"]); -} - -$client = new GraphServiceClient("acquireToken"); +$settings = include('../../tests/Settings.php'); +$client = GraphServiceClient::withClientSecret($settings['TenantName'], $settings['ClientId'], $settings['ClientSecret']); $result = $client->getReports()->getOffice365ActivationCounts()->executeQuery(); var_dump($result->getValue()); diff --git a/src/GraphServiceClient.php b/src/GraphServiceClient.php index 6a4c4dae..53ab68de 100644 --- a/src/GraphServiceClient.php +++ b/src/GraphServiceClient.php @@ -13,6 +13,7 @@ use Office365\OneDrive\Sites\Site; use Office365\Reports\ReportRoot; use Office365\Runtime\Auth\AADTokenProvider; +use Office365\Runtime\Auth\ClientCredential; use Office365\Runtime\Auth\UserCredentials; use Office365\Runtime\ClientRuntimeContext; use Office365\Runtime\Actions\DeleteEntityQuery; @@ -66,6 +67,22 @@ public static function withUserCredentials($tenantName, $clientId, $userName, $p }); } + /** + * @param $tenantName + * @param $clientId + * @param $clientSecret + * @return GraphServiceClient + */ + public static function withClientSecret($tenantName, $clientId, $clientSecret) + { + return new GraphServiceClient(function () use ($clientSecret, $clientId, $tenantName) { + $resource = "https://graph.microsoft.com"; + $provider = new AADTokenProvider($tenantName); + return $provider->acquireTokenForClientCredential($resource, + new ClientCredential($clientId, $clientSecret),["/.default"]); + }); + } + /** * @return ODataRequest */