export const pca = new PublicClientApplication(msalConfig);
// Create an Axios instance
export const sharepointApiCall = axios.create({ baseURL: `${BASE_URL}/_api` });
// MSAL.js v2 exposes several account APIs, logic to determine which account to use is the responsibility of the developer
const account = pca.getAllAccounts()[0];
// Define your access token request configuration
const accessTokenRequest = {
//note: leave this scopes for possible future extension - ms has no docs for the names
// scopes: [
// 'openid',
// 'profile',
// 'email',
// 'allsites.fullcontrol',
// 'allsites.manage',
// 'allsites.read',
// 'allsites.write',
// 'sites.fullcontrol.all',
// 'sites.manage.all',
// 'sites.read.all',
// 'sites.readwrite.all',
// 'user.read',
// 'user.read.all',
// ],
scopes: [`${tenantName}/.default`],
// other token request options
account,
redirectUri: 'http://localhost:3001',
};
// Add an Axios interceptor
sharepointApiCall.interceptors.request.use(async (config) => {
try {
const accessTokenResponse = await pca.acquireTokenSilent(accessTokenRequest);
const accessToken = accessTokenResponse.accessToken;
// Add the token to the request headers
config.headers['Authorization'] = `Bearer ${accessToken}`;
return config;
} catch (error) {
console.error('Error acquiring token:', error);
return Promise.reject(error);
}
});
that scopes: [tenant] is the @kadis solution which works,
token is refreshed and cashed automatically so there is no need to have fancy intercepting - but with this you can more easily call rest API of sharepoint for example with react query and if the error occurs use useMsal to login/logout
hope that helps to anyone in future