Skip to content

Latest commit

 

History

History
88 lines (61 loc) · 4.85 KB

CreatingClientInstance.md

File metadata and controls

88 lines (61 loc) · 4.85 KB

Creating Client Instance

Initialization of the Client can be done in one of below two ways

1. Create With ClientOptions [Recommended]

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.

Option A. Default Middleware chain

The default middleware chain contains consecutively chained instances of the following:

To create a client instance with the default middleware chain:

  1. 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.

  2. 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 -

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);

Option B. Custom Middleware chain

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:

  1. Refer to custom middleware chain for more detailed information.
  2. 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);

2. Create With Options

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);