Skip to content

Instantly share code, notes, and snippets.

@eirikb eirikb/msal-demo.js
Last active Jul 8, 2019

Embed
What would you like to do?
<!DOCTYPE html>
<body>
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.2/js/msal.min.js"></script>
<script>
const azureAdAppClientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
const sharePointUrl = 'https://xxxxxx.sharepoint.com';
const config = {
auth: {clientId: azureAdAppClientId},
cache: {cacheLocation: 'localStorage'}
};
const msal = new Msal.UserAgentApplication(config);
const scopes = [`${sharePointUrl}/Sites.Read.All`];
(async () => {
if (msal.isCallback(window.location.hash)) {
console.log('callback, quit...');
return;
}
msal.handleRedirectCallback((error) => {
if (error) {
console.error(error);
} else {
start();
}
});
if (!msal.getAccount()) {
msal.loginRedirect({scopes});
} else {
start();
}
async function start() {
console.log('Loading token...');
const token = (await msal.acquireTokenSilent({scopes})).accessToken;
console.log(token);
console.log('Loading web...');
const web = await fetch(`${sharePointUrl}/_api/Web`, {
headers: {
accept: 'application/json',
authorization: `Bearer ${token}`
}
}).then(res => res.json());
console.log(web);
}
})();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.