I wish to retrieve token to launch ION Api, but I don't understand how configure the API Gateway and how, in my javscript, open the dialog window to allow access.
I share how i configure the API Gateway and the code where I call the url.
let isFlowInitiated = false;
// Aggiungi l'evento solo una volta
document.getElementById("requestTokenButton").addEventListener("click", handleButtonClick);
function handleButtonClick(event) {
event.stopImmediatePropagation();
document.getElementById("requestTokenButton").removeEventListener("click", handleButtonClick);
const fileInput = document.getElementById("fileInput");
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (event) {
try {
const fileION = JSON.parse(event.target.result);
initiateOAuthFlow(fileION);
document.getElementById('authorization').style.display = 'none';
document.getElementById('importIDM').style.display = 'block';
} catch (error) {
console.error("Errore nel parsing del file:", error);
resetButtonState();
}
};
reader.readAsText(file);
} else {
console.log("No file selected");
document.getElementById('alertMessage').textContent = 'Seleziona un file .ionapi';
$('#alertModal').modal('show');
resetButtonState();
}
}
async function initiateOAuthFlow(fileION) {
if (isFlowInitiated) {
console.warn("OAuth flow is already in progress, avoiding duplicate initiation");
resetButtonState();
return;
}
isFlowInitiated = true;
console.log(window.location.origin);
const authUrl = `${fileION.pu}${fileION.oa}?response_type=code&client_id=${fileION.ci}&redirect_uri=${fileION.ru}`;
try {
const dialog = await openDialog(authUrl);
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg) => {
if (arg.message.startsWith("error:")) {
console.error(arg.message);
alert("Errore durante l'autenticazione: " + arg.message);
} else {
console.log('Received Authorization Code:', arg.message);
}
dialog.close();
isFlowInitiated = false;
resetButtonState();
});
} catch (error) {
console.error('Errore durante l'inizializzazione del flusso OAuth:', error);
isFlowInitiated = false;
resetButtonState();
}
}
function openDialog(url) {
return new Promise((resolve, reject) => {
Office.context.ui.displayDialogAsync(url, { height: 60, width: 30 }, (asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.error('Errore durante l'apertura della finestra di dialogo:', asyncResult.error.message);
reject(asyncResult.error);
} else {
console.log(asyncResult.value)
resolve(asyncResult.value);
}
});
});
}
function resetButtonState() {
console.log("Resetting button state");
// Riaggiungi l'event listener solo dopo il completamento o l'errore del flusso
document.getElementById("requestTokenButton").addEventListener("click", handleButtonClick);
}
I hope that you can help me.