Last active
July 8, 2019 20:38
-
-
Save eirikb/ff4c369f4cf45eebff90d3af6c41b0ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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