Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit ae20e05

Browse files
committed
updated readme incremental
1 parent ad0bca0 commit ae20e05

File tree

2 files changed

+4
-91
lines changed

2 files changed

+4
-91
lines changed

2-Authorization-I/1-call-graph/README-incremental.md

+3-90
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,7 @@ export const Profile = () => {
305305
}
306306

307307
if (result) {
308-
getGraphClient({
309-
account: instance.getActiveAccount(),
310-
scopes: protectedResources.graphMe.scopes,
311-
interactionType: InteractionType.Popup,
312-
claims: claims,
313-
})
308+
getGraphClient(result.accessToken)
314309
.api('/me')
315310
.responseType(ResponseType.RAW)
316311
.get()
@@ -345,7 +340,7 @@ For more details on what's inside the access token, clients should use the token
345340

346341
### Calling the Microsoft Graph API
347342

348-
[Microsoft Graph JavaScript SDK](https://github.com/microsoftgraph/msgraph-sdk-javascript) provides various utility methods to query the Graph API. While the SDK has a default authentication provider that can be used in basic scenarios, it can also be extended to use with a custom authentication provider such as MSAL. To do so, we will initialize the Graph SDK client with [clientOptions](https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CreatingClientInstance.md) method, which contains an `authProvider` object of class **MyAuthenticationProvider** that handles the token acquisition process for the client.
343+
[Microsoft Graph JavaScript SDK](https://github.com/microsoftgraph/msgraph-sdk-javascript) provides various utility methods to query the Graph API. While the SDK has a default authentication provider that can be used in basic scenarios, it can also be extended to use with a custom authentication provider such as MSAL. To do so, we will initialize the Graph SDK client with an [authProvider function](https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CreatingClientInstance.md#2-create-with-options). 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
349344

350345
```javascript
351346
export const getGraphClient = () => {
@@ -356,93 +351,11 @@ For more details on what's inside the access token, clients should use the token
356351
return graphClient;
357352
}
358353
```
359-
360-
**MyAuthenticationProvider** needs to implement the [IAuthenticationProvider](https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/src/IAuthenticationProvider.ts) interface, which can be done as shown below:
361-
362-
```javascript
363-
class MsalAuthenticationProvider {
364-
account; // user account object to be used when attempting silent token acquisition
365-
scopes; // array of scopes required for this resource endpoint
366-
interactionType; // type of interaction to fallback to when silent token acquisition fails
367-
claims; // claims object required when fetching an access token
368-
369-
constructor(providerOptions) {
370-
this.account = providerOptions.account;
371-
this.scopes = providerOptions.scopes;
372-
this.interactionType = providerOptions.interactionType;
373-
this.claims = providerOptions.claims
374-
}
375-
376-
/**
377-
* This method will get called before every request to the ms graph server
378-
* This should return a Promise that resolves to an accessToken (in case of success) or rejects with error (in case of failure)
379-
* Basically this method will contain the implementation for getting and refreshing accessTokens
380-
*/
381-
getAccessToken() {
382-
return new Promise(async (resolve, reject) => {
383-
let response;
384-
385-
try {
386-
response = await msalInstance.acquireTokenSilent({
387-
account: this.account,
388-
scopes: this.scopes,
389-
claims: this.claims
390-
});
391-
392-
if (response.accessToken) {
393-
resolve(response.accessToken);
394-
} else {
395-
reject(Error('Failed to acquire an access token'));
396-
}
397-
} catch (error) {
398-
// in case if silent token acquisition fails, fallback to an interactive method
399-
if (error instanceof InteractionRequiredAuthError) {
400-
switch (this.interactionType) {
401-
case InteractionType.Popup:
402-
response = await msalInstance.acquireTokenPopup({
403-
scopes: this.scopes,
404-
claims: this.claims,
405-
});
406-
407-
if (response.accessToken) {
408-
resolve(response.accessToken);
409-
} else {
410-
reject(Error('Failed to acquire an access token'));
411-
}
412-
break;
413-
case InteractionType.Redirect:
414-
/**
415-
* This will cause the app to leave the current page and redirect to the consent screen.
416-
* Once consent is provided, the app will return back to the current page and then the
417-
* silent token acquisition will succeed.
418-
*/
419-
420-
msalInstance.acquireTokenRedirect({
421-
scopes: this.scopes,
422-
claims: this.claims,
423-
});
424-
break;
425-
426-
default:
427-
break;
428-
}
429-
}
430-
}
431-
});
432-
}
433-
}
434-
```
435-
436354
See [graph.js](./SPA/src/graph.js). The Graph client then can be used in your components as shown below:
437355

438356
```javascript
439357
if (result) {
440-
getGraphClient({
441-
account: instance.getActiveAccount(),
442-
scopes: protectedResources.graphMe.scopes,
443-
interactionType: InteractionType.Popup,
444-
claims: claims,
445-
})
358+
getGraphClient(result.accessToken)
446359
.api('/me')
447360
.responseType(ResponseType.RAW)
448361
.get()

2-Authorization-I/1-call-graph/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ For more details on what's inside the access token, clients should use the token
435435

436436
### Calling the Microsoft Graph API
437437

438-
[Microsoft Graph JavaScript SDK](https://github.com/microsoftgraph/msgraph-sdk-javascript) provides various utility methods to query the Graph API. While the SDK has a default authentication provider that can be used in basic scenarios, it can also be extended to use with a custom authentication provider such as MSAL. To do so, we will initialize the Graph SDK client with an [authProvider function](https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CreatingClientInstance.md#2-create-with-options). 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
438+
[Microsoft Graph JavaScript SDK](https://github.com/microsoftgraph/msgraph-sdk-javascript) provides various utility methods to query the Graph API. While the SDK has a default authentication provider that can be used in basic scenarios, it can also be extended to use with a custom authentication provider such as MSAL. To do so, we will initialize the Graph SDK client with an [authProvider function](https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CreatingClientInstance.md#2-create-with-options). 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.
439439

440440
```javascript
441441
export const getGraphClient = (accessToken) => {

0 commit comments

Comments
 (0)