-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphHelper.php
More file actions
49 lines (38 loc) · 1.85 KB
/
GraphHelper.php
File metadata and controls
49 lines (38 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use Microsoft\Graph\Core\GraphClientFactory;
use Microsoft\Graph\GraphRequestAdapter;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Abstractions\Authentication\BaseBearerTokenAuthenticationProvider;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
require_once 'DeviceCodeTokenProvider.php';
class GraphHelper {
public static function getGraphClientForUser(): GraphServiceClient {
$clientId = $_ENV['CLIENT_ID'];
$tenantId = $_ENV['TENANT_ID'];
$scopes = $_ENV['GRAPH_USER_SCOPES'];
$tokenProvider = new DeviceCodeTokenProvider($clientId, $tenantId, $scopes);
$authProvider = new BaseBearerTokenAuthenticationProvider($tokenProvider);
$adapter = new GraphRequestAdapter($authProvider);
return GraphServiceClient::createWithRequestAdapter($adapter);
}
public static function getDebugGraphClientForUser(): GraphServiceClient {
$clientId = $_ENV['CLIENT_ID'];
$tenantId = $_ENV['TENANT_ID'];
$scopes = $_ENV['GRAPH_USER_SCOPES'];
$logger = new Logger('graph');
$logger->pushHandler(new StreamHandler('php://stdout', \Monolog\Level::Debug));
$stack = GraphClientFactory::getDefaultHandlerStack();
$stack->push(Middleware::log($logger, new MessageFormatter('{method} {uri} {req_body}')));
$httpClient = GraphClientFactory::createWithMiddleware($stack);
$tokenProvider = new DeviceCodeTokenProvider($clientId, $tenantId, $scopes);
$authProvider = new BaseBearerTokenAuthenticationProvider($tokenProvider);
$adapter = new GraphRequestAdapter($authProvider, $httpClient);
return GraphServiceClient::createWithRequestAdapter($adapter);
}
}
?>