microsoft / microsoft-graph-beta
The Microsoft Graph Beta SDK for PHP
Installs: 19 635
Dependents: 1
Suggesters: 0
Security: 1
Stars: 14
Watchers: 15
Forks: 4
Open Issues: 4
Requires
- php: ^8.0 || ^7.4
- microsoft/microsoft-graph-core: ^2.1.0
Requires (Dev)
- phpstan/phpstan: ^0.12.90 || ^1.0.0
- phpunit/phpunit: ^8.0 || ^9.0
- dev-main
- v2.20.0
- v2.19.0
- v2.18.0
- v2.17.1
- v2.17.0
- v2.16.0
- v2.15.0
- v2.14.0
- v2.13.0
- v2.12.0
- v2.11.0
- 2.10.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- 2.0.0-RC28
- 2.0.0-RC27
- 2.0.0-RC26
- 2.0.0-RC25
- 2.0.0-RC24
- 2.0.0-RC23
- 2.0.0-RC22
- 2.0.0-RC21
- 2.0.0-RC20
- 2.0.0-RC19
- 2.0.0-RC18
- 2.0.0-RC17
- 2.0.0-RC16
- 2.0.0-RC15
- 2.0.0-RC14
- 2.0.0-RC13
- 2.0.0-RC12
- 2.0.0-RC11
- 2.0.0-RC10
- 2.0.0-RC9
- 2.0.0-RC8
- 2.0.0-RC7
- 2.0.0-RC6
- 2.0.0-RC5
- 2.0.0-RC4
- 2.0.0-RC3
- 2.0.0-RC2
- 2.0.0-RC1
- dev-kiota/beta/pipelinebuild/171412
- dev-kiota/beta/pipelinebuild/171184
- dev-kiota/beta/pipelinebuild/171177
- dev-release-please--branches--main--components--microsoft/microsoft-graph-beta
- dev-kiota/beta/pipelinebuild/167322
- dev-kiota/beta/pipelinebuild/167170
- dev-kiota/beta/pipelinebuild/163658
- dev-kiota/beta/pipelinebuild/160559
- dev-kiota/beta/pipelinebuild/160542
- dev-kiota/beta/pipelinebuild/160533
This package is auto-updated.
Last update: 2024-11-19 11:55:02 UTC
README
Install the SDK
You can install the Beta PHP SDK with Composer by editing your composer.json
file:
{
"require": {
// x-release-please-start-version
"microsoft/microsoft-graph-beta": "^2.20.0"
// x-release-please-end
}
}
Get started with Microsoft Graph
Register your application
Register your application to use the Microsoft Graph API using Microsoft Azure Active Directory in your tenant's Active Directory to support work or school users for your tenant, or multiple tenants.
Create a Token Request Context
A Token Request Context contains the credentials used to authenticate requests. The SDK supports various contexts that align with OAuth 2.0 flows: client_credentials
, authorization_code
and on_behalf_of
with support for secret-based and certificate-based client authentication.
The Token Request Context is passed to an authentication provider which fetches, caches and refreshes access tokens ensuring all requests are authenticated against the Microsoft Identity platform.
The following sample creates a TokenRequestContext that gets access without a user:
<?php use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext; use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider; $tokenRequestContext = new ClientCredentialContext( 'tenantId', 'clientId', 'clientSecret' );
To gets access on behalf of a user:
<?php use Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext; use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider; $tokenRequestContext = new AuthorizationCodeContext( 'tenantId', 'clientId', 'clientSecret', 'authCode', 'redirectUri' );
Note that your application will need to handle redirecting the user to the Microsoft Identity login page to get the authorization_code
that's passed into the AuthorizationCodeContext
.
See for more on the authorization_code
grant flow.
To keep your user signed in across multiple requests within a session, see section on access token management
Initialise a GraphServiceClient
Using the Token Request Context and optional scopes, a GraphServiceClient
can be initialised:
use Microsoft\Graph\GraphServiceClient; // Defaults to using https://graph.microsoft.com/.default scopes $graphServiceClient = new GraphServiceClient($tokenRequestContext); // With specific scopes $scopes = ['User.Read', 'Mail.ReadWrite']; $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
To initialize the GraphServiceClient
with an already acquired access token or to retrieve the access token that the SDK fetches on your behalf, see section on access token management.
For more on Graph client configuration, see more examples
Call Microsoft Graph using the Beta endpoint and models
The following is an example that shows how to fetch a user from Microsoft Graph
use Microsoft\Graph\Beta\GraphServiceClient; use Microsoft\Kiota\Abstractions\ApiException; use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext; $tokenRequestContext = new ClientCredentialContext( 'tenantId', 'clientId', 'clientSecret' ); $betaGraphServiceClient = new GraphServiceClient($tokenRequestContext); try { $user = $betaGraphServiceClient->users()->byUserId('[userPrincipalName]')->get()->wait(); echo "Hello, I am {$user->getGivenName()}"; } catch (ApiException $ex) { echo $ex->getError()->getMessage(); }
Note that to calling me()
requires a signed-in user and therefore delegated permissions (obtained using the authorization_code
flow):
use Microsoft\Graph\Beta\GraphServiceClient; use Microsoft\Kiota\Abstractions\ApiException; use Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext; $tokenRequestContext = new AuthorizationCodeContext( 'tenantId', 'clientId', 'clientSecret', 'authCode', 'redirectUri' ); $scopes = ['User.Read']; $betaGraphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes); try { $user = $betaGraphServiceClient->me()->get()->wait(); echo "Hello, I am {$user->getGivenName()}"; } catch (ApiException $ex) { echo $ex->getError()->getMessage(); }
Documentation and resources
Upgrading
For detailed information on breaking changes, bug fixes and new functionality introduced during major upgrades, check out our Upgrade Guide
Develop
Run Tests
Run
vendor/bin/phpunit
from the base directory.
Issues
View or log issues on the Issues tab in the repo.
Contribute
Please read our Contributing guidelines carefully for advice on how to contribute to this repo.
Copyright and license
Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.