Skip to content

Commit 96dd842

Browse files
Update javascript file
1 parent 154c45a commit 96dd842

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

script.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,48 @@
44
const siteUrl = "https://usace.dps.mil/sites/TDL-CEIWR-RMC-ALL";
55
const listName = "LibraryBooks";
66

7+
// Define your Azure AD credentials
8+
const tenantId = "<Your Tenant ID>"; // Replace with your Tenant ID
9+
const clientId = "<Your Client ID>"; // Replace with your Client ID
10+
const clientSecret = "<Your Client Secret>"; // Replace with your Client Secret
11+
const resource = "https://usace.dps.mil"; // SharePoint resource URL
12+
13+
// Function to obtain an access token
14+
async function getAccessToken() {
15+
const tokenUrl = `https://login.microsoftonline.com/${tenantId}/oauth2/token`;
16+
17+
const response = await fetch(tokenUrl, {
18+
method: "POST",
19+
headers: {
20+
"Content-Type": "application/x-www-form-urlencoded"
21+
},
22+
body: new URLSearchParams({
23+
grant_type: "client_credentials",
24+
client_id: clientId,
25+
client_secret: clientSecret,
26+
resource: resource
27+
})
28+
});
29+
30+
const data = await response.json();
31+
if (data.error) {
32+
console.error("Error obtaining access token:", data.error_description);
33+
throw new Error(data.error_description);
34+
}
35+
return data.access_token;
36+
}
37+
738
// Fetch books from SharePoint
839
async function fetchBooks() {
40+
const accessToken = await getAccessToken(); // Get the access token
41+
942
const response = await fetch(`${siteUrl}/_api/web/lists/getbytitle('${listName}')/items`, {
1043
headers: {
44+
"Authorization": `Bearer ${accessToken}`, // Use the access token
1145
"Accept": "application/json;odata=verbose"
1246
}
1347
});
48+
1449
const data = await response.json();
1550
return data.d.results;
1651
}
@@ -38,12 +73,14 @@ async function checkOutBook(bookId) {
3873
const userName = prompt("Enter your name:");
3974
if (!userName) return alert("Name is required!");
4075

76+
const accessToken = await getAccessToken(); // Get the access token
77+
4178
await fetch(`${siteUrl}/_api/web/lists/getbytitle('${listName}')/items(${bookId})`, {
4279
method: "POST",
4380
headers: {
81+
"Authorization": `Bearer ${accessToken}`, // Use the access token
4482
"Accept": "application/json;odata=verbose",
45-
"Content-Type": "application/json;odata=verbose",
46-
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value
83+
"Content-Type": "application/json;odata=verbose"
4784
},
4885
body: JSON.stringify({
4986
Status: "Checked Out",
@@ -57,12 +94,14 @@ async function checkOutBook(bookId) {
5794

5895
// Check in a book
5996
async function checkInBook(bookId) {
97+
const accessToken = await getAccessToken(); // Get the access token
98+
6099
await fetch(`${siteUrl}/_api/web/lists/getbytitle('${listName}')/items(${bookId})`, {
61100
method: "POST",
62101
headers: {
102+
"Authorization": `Bearer ${accessToken}`, // Use the access token
63103
"Accept": "application/json;odata=verbose",
64-
"Content-Type": "application/json;odata=verbose",
65-
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value
104+
"Content-Type": "application/json;odata=verbose"
66105
},
67106
body: JSON.stringify({
68107
Status: "Available",

0 commit comments

Comments
 (0)