Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
O365-ADALJS-examples/Authenticate-an-Office-365-user-with-ADAL-JS/Authenticate-an-Office-365-user-with-ADAL-JS.html
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
118 lines (109 sloc)
3.78 KB
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> | |
<html> | |
<head> | |
<title>Authenticate an Office 365 user with ADAL JS</title> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> | |
<style>body{font:normal normal normal 14px/1.5em "Century Gothic", sans-serif;}</style> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.0/js/adal.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
"use strict"; | |
// Assign variables | |
var variables = { | |
// Domain of Azure AD tenant | |
azureAD: "tenantname.onmicrosoft.com", | |
// ClientId of Azure AD application principal | |
clientId: "11111111-1111-1111-1111-111111111111", | |
// GUID of SharePoint list | |
listId: "22222222-2222-2222-2222-222222222222", | |
// Name of SharePoint tenant | |
sharePointTenant: "tenantname" | |
} | |
// Create config and get AuthenticationContext | |
window.config = { | |
tenant: variables.azureAD, | |
clientId: variables.clientId, | |
postLogoutRedirectUri: window.location.origin, | |
endpoints: { | |
graphApiUri: "https://graph.microsoft.com", | |
sharePointUri: "https://" + variables.sharePointTenant + ".sharepoint.com", | |
}, | |
cacheLocation: "localStorage" | |
}; | |
var authContext = new AuthenticationContext(config); | |
var isCallback = authContext.isCallback(window.location.hash); | |
authContext.handleWindowCallback(); | |
if (isCallback && !authContext.getLoginError()) { | |
window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST); | |
} | |
var user = authContext.getCachedUser(); | |
if (!user) { | |
authContext.login(); | |
} | |
// Get OneDrive documents of current user with AuthenticationContext of Graph API | |
authContext.acquireToken(config.endpoints.graphApiUri, function (error, token) { | |
if (error || !token) { | |
console.log("ADAL error occurred: " + error); | |
return; | |
} | |
else { | |
var filesUri = config.endpoints.graphApiUri + "/v1.0/me/drive/root/children"; | |
$.ajax({ | |
type: "GET", | |
url: filesUri, | |
headers: { | |
"Authorization": "Bearer " + token | |
} | |
}).done(function (response) { | |
console.log("Successfully fetched files from OneDrive."); | |
var items = response.value; | |
for (var i = 0; i < items.length; i++){ | |
console.log(items[i].name); | |
$("#OneDrive").append("<li>" + items[i].name + "</li>"); | |
} | |
}).fail(function () { | |
console.log("Fetching files from OneDrive failed."); | |
}); | |
} | |
}); | |
// Get SharePoint documents of list with AuthenticationContext of SharePoint | |
authContext.acquireToken(config.endpoints.sharePointUri, function (error, token) { | |
if (error || !token) { | |
console.log("ADAL error occurred: " + error); | |
return; | |
} | |
else { | |
var listUri = config.endpoints.sharePointUri + "/_api/web/lists('" + variables.listId + "')/items?$select=Title"; | |
$.ajax({ | |
type: "GET", | |
url: listUri, | |
headers: { | |
"Authorization": "Bearer " + token, | |
"accept": "application/json;odata=verbose" | |
} | |
}).done(function (response) { | |
console.log("Successfully fetched list from SharePoint."); | |
var items = response.d.results; | |
for (var i = 0; i < items.length; i++){ | |
console.log(items[i].Title); | |
$("#SharePoint").append("<li>" + items[i].Title + "</li>"); | |
} | |
}).fail(function () { | |
console.log("Fetching list from SharePoint failed."); | |
}); | |
} | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Authenticate an Office 365 user with ADAL JS</h1> | |
<h2>OneDrive documents</h2> | |
<ul id="OneDrive"> | |
</ul> | |
<h2>SharePoint documents</h2> | |
<ul id="SharePoint"> | |
</ul> | |
</body> | |
</html> |