|
| 1 | +#### Creating an instance of AuthCodeMSALBrowserAuthenticationProvider for a browser application |
| 2 | + |
| 3 | +**Note**: The `AuthCodeMSALBrowserAuthenticationProvider` is introduced in version 3.0.0 of Microsoft Graph Client Library |
| 4 | + |
| 5 | +###### Links for more information - |
| 6 | + |
| 7 | +- [npm - @azure/msal-browser](https://www.npmjs.com/package/@azure/msal-browser) |
| 8 | +- [github - @azure/msal-browser](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/README.md) |
| 9 | + |
| 10 | +Steps to use `AuthCodeMSALBrowserAuthenticationProvider`; |
| 11 | + |
| 12 | +1. Using npm: `npm install @azure/msal-browser @microsoft/microsoft-graph-client` |
| 13 | + |
| 14 | + Using html: |
| 15 | + |
| 16 | + ```html |
| 17 | + <!--Using script tag to include the bundled file or the CDN source--> |
| 18 | + <script type="text/javascript" src="https://alcdn.msauth.net/browser/2.15.0/js/msal-browser.min.js"></script> |
| 19 | + <script src="graph-js-sdk.js"></script> |
| 20 | + <script src="graph-client-msalBrowserAuthProvider.js"></script> |
| 21 | + ``` |
| 22 | + |
| 23 | + Reference : [MSAL Browser CDN usage](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/cdn-usage.md) |
| 24 | + |
| 25 | +2. Initialize the `msal-browser` `PublicClientApplication` instance: Learn more [how to initialize msal](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/initialization.md) |
| 26 | + |
| 27 | +3. Following sample shows how to initialize a Microsoft Graph SDK `Client` instance, |
| 28 | + |
| 29 | +Using npm: |
| 30 | + |
| 31 | +```typescript |
| 32 | + import { PublicClientApplication, InteractionType, AccountInfo } from "@azure/msal-browser"; |
| 33 | + |
| 34 | + import { AuthCodeMSALBrowserAuthenticationProvider, AuthCodeMSALBrowserAuthenticationProviderOptions } from "@microsoft/microsoft-graph-client/authProviders/authCodeMsalBrowser"; |
| 35 | + |
| 36 | + const options:AuthCodeMSALBrowserAuthenticationProviderOptions: { |
| 37 | + account: account, // the AccountInfo instance to acquire the token for. |
| 38 | + interactionType: InteractionType.PopUp , // msal-browser InteractionType |
| 39 | + scopes: ["user.read", "mail.send"] // example of the scopes to be passed |
| 40 | + } |
| 41 | + |
| 42 | + // Pass the PublicClientApplication instance from step 2 to create AuthCodeMSALBrowserAuthenticationProvider instance |
| 43 | + const authProvider: new AuthCodeMSALBrowserAuthenticationProvider(publicClientApplication, options), |
| 44 | + |
| 45 | + |
| 46 | + // Initialize the Graph client |
| 47 | + const graphClient = Client.initWithMiddleware({ |
| 48 | + authprovider |
| 49 | + }); |
| 50 | + |
| 51 | +``` |
| 52 | + |
| 53 | +Using CDN or script: |
| 54 | + |
| 55 | +```javascript |
| 56 | +const msalClient = new msal.PublicClientApplication(msalConfig); |
| 57 | + |
| 58 | +const authProvider = new MSGraphAuthCodeMSALBrowserAuthProvider.AuthCodeMSALBrowserAuthenticationProvider(msalClient, { |
| 59 | + account, // the AccountInfo instance to acquire the token for |
| 60 | + scopes: ["user.read", "mail.send"], |
| 61 | + interactionType: msal.InteractionType.Popup, |
| 62 | +}); |
| 63 | + |
| 64 | +// Initialize the Graph client |
| 65 | +const graphClient = MicrosoftGraph.Client.initWithMiddleware({ authProvider }); |
| 66 | +``` |
0 commit comments