Failed to load destination. Caused by: No user token (JWT) has been provided. This is strictly neces

Hi, I am trying to call sap odata service from a cap application uisnng cds spawn job.Using  destination with PrincipalPropagation, But geeting below error: –

logs:-
[cds] – connect to sap_odata > odata-v2 { destination: ‘sap_destination’, forwardAuthToken: true }
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR [remote] – Error: Error during request to remote service:
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR Failed to load destination.
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at run (/home/vcap/app/node_modules/@sap/cds/libx/_runtime/remote/utils/client.js:310:31)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async RemoteService.<anonymous> (/home/vcap/app/node_modules/@sap/cds/libx/_runtime/remote/Service.js:276:20)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async next (/home/vcap/app/node_modules/@sap/cds/lib/srv/srv-dispatch.js:76:17)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async RemoteService.handle (/home/vcap/app/node_modules/@sap/cds/lib/srv/srv-dispatch.js:74:10)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async Object.methodName(/home/vcap/app/srv/util/myfile.js:102:24)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async mycatfile (/home/vcap/app/srv/util/mycatfile.js:524:22) {
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR statusCode: 502,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR reason: {
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR message: ‘Error during request to remote service: n’ +
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR “Failed to load destination. Caused by: No user token (JWT) has been provided. This is strictly necessary for ‘PrincipalPropagation’.”,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR request: {
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR method: ‘POST’,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR url: ‘/xxxx/xxxxxx_SRV/dummyEntitySet’,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR headers: {
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR accept: ‘application/json,text/plain’,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR ‘accept-language’: ‘en’,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR ‘content-type’: ‘application/json’,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR ‘content-length’: 311,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR authorization: ‘Bearer …’
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR }
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR },
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR correlationId: ‘507a3d4f-bf6d-4296-4cc1-f6b8f070ee2b’
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR }
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR } 

my code :- 

 

cds.spawn({
user: cds.context?.user,
tenant: cds.context?.tenant,
headers: cds.context?.headers || req?.headers,
after: iafterTime }, async (req) => {
sendToSAP(req, customID)
})

const sendToSAP = async (req, customID) => {
const payload = {}
const oSAPService = await cds.connect.to(“sap_destination”)
let headers = { “Content-Type”: “application/json” }
headers.authorization = cds.context?.headers || req?.headers
const response = await oSAPService.send({
method: “POST”,
path: “/xxxx/xxxxxx_SRV/dummyEntitySet”,
data: payload,
headers: headers
});
}

 

dependencies :- 

“node”: “^18.0.0”
“@sap/cds”: “6.8.4”

 

Scroll to Top