Skip to content

Commit d32d8fb

Browse files
committed
GraphServiceClient::withUserCredentials method introduced
1 parent c89f38a commit d32d8fb

File tree

4 files changed

+33
-35
lines changed

4 files changed

+33
-35
lines changed

examples/OneNote/ListPages.php

+3-11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@
77
use Office365\Runtime\Auth\AADTokenProvider;
88
use Office365\Runtime\Auth\UserCredentials;
99

10-
function acquireToken()
11-
{
12-
$settings = include( '../../tests/Settings.php');
13-
$resource = "https://graph.microsoft.com";
14-
$provider = new AADTokenProvider($settings['TenantName']);
15-
return $provider->acquireTokenForPassword($resource, $settings['ClientId'],
16-
new UserCredentials($settings['UserName'], $settings['Password']));
17-
}
18-
19-
20-
$client = new GraphServiceClient("acquireToken");
10+
$settings = include( '../../tests/Settings.php');
11+
$client = GraphServiceClient::withUserCredentials(
12+
$settings['TenantName'], $settings['ClientId'], $settings['UserName'], $settings['Password']);
2113

2214
$pages = $client->getMe()->getOneNote()->getPages()->get()->executeQuery();
2315
/** @var OnenotePage $page */

examples/Outlook/ListCals.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88

99
require_once '../vendor/autoload.php';
1010

11-
function acquireToken()
12-
{
13-
$resource = "https://graph.microsoft.com";
14-
$settings = include('../../tests/Settings.php');
15-
$provider = new AADTokenProvider($settings['TenantName']);
16-
return $provider->acquireTokenForPassword($resource, $settings['ClientId'],
17-
new UserCredentials($settings['UserName'], $settings['Password']));
18-
}
19-
20-
$client = new GraphServiceClient("acquireToken");
11+
$settings = include('../../tests/Settings.php');
12+
$client = GraphServiceClient::withUserCredentials(
13+
$settings['TenantName'], $settings['ClientId'], $settings['UserName'], $settings['Password']
14+
);
2115

2216
$userCals = $client->getMe()->getCalendars()->get()->executeQuery();
2317
/** @var Calendar $cal */

src/GraphServiceClient.php

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Office365\OneDrive\Drives\DriveCollection;
1313
use Office365\OneDrive\Sites\Site;
1414
use Office365\Reports\ReportRoot;
15+
use Office365\Runtime\Auth\AADTokenProvider;
16+
use Office365\Runtime\Auth\UserCredentials;
1517
use Office365\Runtime\ClientRuntimeContext;
1618
use Office365\Runtime\Actions\DeleteEntityQuery;
1719
use Office365\Runtime\Http\HttpMethod;
@@ -46,6 +48,24 @@ public function __construct(callable $acquireToken)
4648
parent::__construct();
4749
}
4850

51+
/**
52+
* Initializes a client to acquire a token via user credentials.
53+
* @param $tenantName
54+
* @param $clientId
55+
* @param $userName
56+
* @param $password
57+
* @return GraphServiceClient
58+
*/
59+
public static function withUserCredentials($tenantName, $clientId, $userName, $password)
60+
{
61+
return new GraphServiceClient(function () use ($password, $userName, $clientId, $tenantName) {
62+
$resource = "https://graph.microsoft.com";
63+
$provider = new AADTokenProvider($tenantName);
64+
return $provider->acquireTokenForPassword($resource, $clientId,
65+
new UserCredentials($userName, $password));
66+
});
67+
}
68+
4969
/**
5070
* @return ODataRequest
5171
*/

tests/GraphTestCase.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Office365;
44

5-
use Office365\Runtime\Auth\AADTokenProvider;
6-
use Office365\Runtime\Auth\UserCredentials;
75
use PHPUnit\Framework\TestCase;
86

97

@@ -25,25 +23,19 @@ public static function setUpBeforeClass(): void
2523
{
2624
self::$settings = include(__DIR__ . '/Settings.php');
2725
self::$testAccountName = self::$settings['TestAccountName'];
28-
self::$graphClient = new GraphServiceClient(function () {
29-
return self::acquireToken();
30-
});
26+
self::$graphClient = GraphServiceClient::withUserCredentials(
27+
self::$settings['TenantName'],
28+
self::$settings['ClientId'],
29+
self::$settings['UserName'],
30+
self::$settings['Password']
31+
);
3132
}
3233

3334
public static function tearDownAfterClass(): void
3435
{
3536
self::$graphClient = null;
3637
}
3738

38-
39-
public static function acquireToken()
40-
{
41-
$resource = "https://graph.microsoft.com";
42-
$provider = new AADTokenProvider(self::$settings['TenantName']);
43-
return $provider->acquireTokenForPassword($resource, self::$settings['ClientId'],
44-
new UserCredentials(self::$settings['UserName'], self::$settings['Password']));
45-
}
46-
4739
public static function createUniqueName($prefix){
4840
return $prefix . "_" . rand(1, 100000);
4941
}

0 commit comments

Comments
 (0)