You can call third party API protected through Azure Ad using following given approach.
- Declare permission requests in SharePoint Framework projects
- To request a permission when a SharePoint Framework package is deployed to the App Catalog, add it to the webApiPermissionRequests array in the package-solution.json
// package-solution.json
{
"solution": {
"name": "sp-fx-aad-http-client-side-solution",
"id": "dfb230b7-4f61-431f-9b65-a34e83922663",
"version": "1.0.0.0",
"includeClientSideAssets": true,
"isDomainIsolated": false,
"webApiPermissionRequests": [
{
"resource": "Microsoft Graph",
"scope": "User.ReadBasic.All"
}
]
},
"paths": {
"zippedPackage": "solution/sp-fx-aad-http.sppkg"
}
}
Grant the permission to app from SharePoint admin portal.
Use the SharePoint Framework Azure AD HTTP client
- The SharePoint Framework API simplifies the access token acquisition from SharePoint Online and Azure AD. The API uses the token to configure a special instance of the
HttpClient
, known as the AadHttpClient
, you'll use to submit the request.
- Use below sample of code to call the third party api.
import {
AadHttpClient,
HttpClientResponse
} from '@microsoft/sp-http';
// Promises
this.context.aadHttpClientFactory
.getClient('https://your-endpoint-uri')
.then((aadClient: AadHttpClient) => {
/* submit request to endpoint */
});
// Async/await
const aadClient: AadHttpClient = await this.context.aadHttpClientFactory
.getClient('https://your-endpoint-uri');
// Promises
const endpoint: string = 'https://your-endpoint-uri/api';
aadClient.get(endpoint, AadHttpClient.configurations.v1)
.then((rawResponse: HttpClientResponse) => {
return rawResponse.json();
})
.then((jsonResponse: any) => {
// work with result
});
// Async/await
const endpoint: string = 'https://your-endpoint-uri/api';
const rawResponse: HttpClientResponse = await aadClient.get(endpoint, AadHttpClient.configurations.v1);
const responseJson = await rawResponse.json();
return responseJson as any;
Reference