Initialization of the Client can be done in one of below two ways
The Microsoft Graph SDK client configures a default set of middleware that allows the SDK to communicate with the Microsoft Graph endpoints. This default set is customizable, allowing you to change the behavior of the client
In order to instantiate a Client object, one has to pass in the authProvider
or middleware
chain in ClientOptions.
The default middleware chain contains consecutively chained instances of the following:
To create a client instance with the default middleware chain:
-
Create an instance of a class which implements AuthenticationProvider interface. This class should contain the logic to get the access token to be passed to the Microsoft Graph API.
-
Pass the instance as
authProvider
in ClientOptions to instantiate the Client which will create and set the default middleware chain.
let clientOptions: ClientOptions = {
authProvider: new YourAuthProviderClass(),
};
const client = Client.initWithMiddleware(clientOptions);
The Microsoft Graph JavaScript Client Library has an adapter implementation for the following -
-
(TokenCredentialAuthenticationProvider) to support Azure Identity TokenCredential (Azure Identity client library for JavaScript) which takes care of getting the
accessToken
. @azure/identity library does not ship with this library, user has to include it externally (For including @azure/identity, refer this).Learn how to create an instance of TokenCredentialAuthenticationProvider.
-
(AuthCodeMSALBrowserAuthenticationProvider for msal-browser (Microsoft Authentication Library) which takes care of getting the
accessToken
.msal-browser
library does not ship with this library, user has to include it externally.Learn how to create an instance of AuthCodeMSALBrowserAuthenticationProvider.
User can integrate any preferred authentication library by implementing IAuthenticationProvider
interface. Refer implementing Custom Authentication Provider for more detailed information.
let clientOptions: ClientOptions = {
// MyCustomAuthenticationProvider is the user's own authentication provider implementing AuthenticationProvider interface
authProvider: new MyCustomAuthenticationProvider(),
};
const client = Client.initWithMiddleware(clientOptions);
The Microsoft Graph SDK client allows configuring custom middleware, allowing you to change the behavior of the client. For example, you can insert customized logging, or add a test handler to simulate specific scenarios.
To create a client instance with the custom middleware chain:
- Refer to custom middleware chain for more detailed information.
- Create the middleware chain and pass first middleware in the chain as
middleware
in ClientOptions.
let clientOptions: ClientOptions = {
// MyFirstMiddleware is the first middleware in my custom middleware chain
middleware: new MyFirstMiddleware(),
};
const client = Client.initWithMiddleware(clientOptions);
Pass an authProvider function in Options while initializing the Client. In this case, user has to provide their own implementation for getting and refreshing accessToken. A callback will be passed into this authProvider function, accessToken or error needs to be passed in to that callback.
// Some callback function
const authProvider: AuthProvider = (callback: AuthProviderCallback) => {
// Your logic for getting and refreshing accessToken
// Error should be passed in case of error while authenticating
// accessToken should be passed upon successful authentication
callback(error, accessToken);
};
let options: Options = {
authProvider,
};
const client = Client.init(options);