Hi,
I have tried various approaches now, but failed every time to establish a connection to MS Graph through a client secret..
There are two scenarios I'd like to cover:
- Send an email from a user mailbox
- Store and retrieve files from Sharepoint
I understand that I have to execute those under a permissioned user, so I would need to first fetch the user and then execute everything else from that user. However, I don't even get there right now... I have registered my application to have application level rights for User.ReadAll and Mail.Read / ReadWrite etc.
I managed to get an access token by calling the API directly via GuzzleHttp through some tutorial I found. However, that is then using the Graph class to execute further and it seems that does not exist in the PHP SDK, instead it is a GraphServiceClient
If I follow the instructions to use GraphServiceClient, I always get an "invalid scope" error. Here is how the code looks, what am I doing wrong??
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider;
$tokenRequestContext = new ClientCredentialContext(
'tenantId',
'clientId',
'clientSecret'
);
use Microsoft\Graph\GraphServiceClient;
// With specific scopes
$scopes = ['User.ReadAll'];
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
try {
$user = $graphServiceClient->users()->byUserId('UPN goes here')->get()->wait();
echo "Hello, I am {$user->getGivenName()}";
} catch (ApiException $ex) {
echo $ex->getError()->getMessage();
}