Skip to content

Instantly share code, notes, and snippets.

@eirikb
Last active July 8, 2019 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eirikb/ff4c369f4cf45eebff90d3af6c41b0ba to your computer and use it in GitHub Desktop.
Save eirikb/ff4c369f4cf45eebff90d3af6c41b0ba to your computer and use it in GitHub Desktop.
<!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