const axios = require('axios');
const parse = require('url-parse');
const querystring = require('querystring');
// BEGIN - Configuration parameters
const CLIENT_ID = '92bec80fe76447768d36b9dfd2f0a179';
const CLIENT_SECRET = 'p8e-tZi5e6_EUylu8UCTcXYMfJ81fRnFsQfr';
const imsOrg = '6BB915785DE67ED10A495E68@AdobeOrg';
const sandboxName = 'sandbox02';
const containerId = '75d8dab4-2a7b-3781-a9db-c1601882f429';
// For Decisioning API
const propositions = [
{
"xdm:activityId": "xcore:offer-activity:170f506848020557",
"xdm:placementId": "xcore:offer-placement:170e802b02f9063b"
}
];
// END - Configuration parameters
// Define the Adobe OAuth 2.0 token URL
const tokenUrl = 'https://ims-na1.adobelogin.com/ims/token/v3';
// Function to retrieve the Bearer Token
async function getBearerToken() {
try {
// Prepare the token request data
const tokenRequestData = {
grant_type: 'client_credentials',
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
scope: 'openid,AdobeID,read_organizations,additional_info.projectedProductContext,session,additional_info.job_function,additional_info.roles',
};
// Send a POST request to the Adobe token URL
const response = await axios.post(tokenUrl, querystring.stringify(tokenRequestData), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
if (response.data && response.data.access_token) {
const accessToken = response.data.access_token;
return accessToken;
} else {
console.error('Unable to retrieve access token.');
return null;
}
} catch (error) {
console.error('Error while retrieving access token:', error);
return null;
}
}
// Function to retrieve decisioning offers using the Bearer Token
async function getDecisioningOffersWithToken(profileIds) {
const accessToken = await getBearerToken();
const profiles = [
{
"xdm:identityMap": {
}
}
];
console.log(profileIds)
if (profileIds.email !== undefined) {
profiles[0]["xdm:identityMap"].Email = [
{
"xdm:id": profileIds.email
}
]
}
if (profileIds.ecid !== undefined) {
profiles[0]["xdm:identityMap"].ECID = [
{
"xdm:id": profileIds.ecid
}
]
}
if (accessToken) {
try {
const url = `https://platform.adobe.io/data/core/ode/${containerId}/decisions`;
const headers = {
'Accept': 'application/vnd.adobe.xdm+json; schema="https://ns.adobe.com/experience/offer-management/decision-response;version=1.0"',
'Content-Type': 'application/vnd.adobe.xdm+json; schema="https://ns.adobe.com/experience/offer-management/decision-request;version=1.0"',
'Authorization': `Bearer ${accessToken}`,
'x-api-key': CLIENT_ID,
'x-gw-ims-org-id': imsOrg,
'x-sandbox-name': sandboxName,
};
const data = {
"xdm:propositionRequests": propositions,
"xdm:profiles": profiles,
"xdm:allowDuplicatePropositions": {
"xdm:acrossActivities": true,
"xdm:acrossPlacements": true
},
"xdm:responseFormat": {
"xdm:includeContent": true,
"xdm:includeMetadata": {
"xdm:activity": [
"name"
],
"xdm:option": [
"name"
],
"xdm:placement": [
"name"
]
}
}
};
const response = await axios.post(url, data, { headers })
if (response.data != undefined) {
console.log('Response data:', response.data);
return response.data
}
else {
console.error('Error:', error);
};
} catch (error) {
// Handle errors here
console.error('Error:', error);
}
}
}
// Call the getDecisioningOffersWithToken function to retrieve decisioning offers using the Bearer Token
exports.endpoint = async function (request, response) {
let url = parse(request.url, true);
let offers = await getDecisioningOffersWithToken({
email: url.query.email,
ecid: url.query.ecid
})
// Return only the content of the first offer
try {
const deliveryURLs = [];
const propositions = offers['xdm:propositions'];
for (const proposition of propositions) {
const options = proposition['xdm:options'];
for (const option of options) {
const deliveryURL = option['xdm:deliveryURL'];
deliveryURLs.push(deliveryURL);
}
}
response.end(JSON.stringify({ deliverURL: deliveryURLs[0] }))
}
catch (e) {
response.end(JSON.stringify(e))
}
}