I want to use the Management API of my CMS to create entries when users fill in a Netcall form.
I have tried using a Fragment Callback and my code is like this:
return {
main: function (myObj) {
// Secrets redacted!
const PROJECT = 'website';
const id = '';
const shared = '';
// Get an authorization token.
async function getAuth() {
let auth = fetch(
'https://cms-chesheast.cloud.contensis.com/authenticate/connect/token',
{
method: 'post',
body: `grant_type=client_credentials&client_id=${id}&client_secret=${shared}&scope=Entry_Read ContentType_Read Project_Read Entry_Write`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
}
)
.then((response) => response.json())
.then((data) => {
return data.access_token;
});
return auth;
}
async function sendComment(entry) {
let response = getAuth().then((auth) => {
fetch(
'https://cms-chesheast.cloud.contensis.com/api/management/projects/website/entries',
{
method: 'post',
headers: {
Authorization: 'bearer ' + auth,
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify(entry),
}
);
});
}
// Create an entry.
const values = myObj.get_values();
let date = new Date();
let msg = values[cs.ref('TestEntryCreation')];
let newEntry = {
myComment: msg,
dateAndTime: date,
sys: {
contentTypeId: 'testComment',
projectId: PROJECT,
language: 'en-GB',
dataFormat: 'entry',
},
};
sendComment(newEntry);
}
}
It isn’t working although the same code works in a local environment.
Any help appreciated.
Mark