cancel
Showing results for 
Search instead for 
Did you mean: 

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

satya-dev
Participant
0 Kudos
1,528

Hi, I am attempting to call an SAP OData service from a CAP (Cloud Application Programming) application using a CDS spawn job. I am utilizing a destination with Principal Propagation but encountering the following 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 } 

code :- 

 

const myFn = async (req, data) {
    cds.spawn({ 
          user: cds.context?.user,
          tenant: cds.context?.tenant,
          headers: cds.context?.headers || req?.headers,
          after: iafterTime }, async (req) => {
          sendToSAP(req, customID)
    })
    return 'processing in backgorund'
}

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": {
"@sap-cloud-sdk/http-client": "^3.0.1",
"@sap/cds": "6.8.4",
"@sap/cds-dk": "6.8.3",
},
"engines": {
"node": "^18.0.0"
}
 
this is workign without spawn job. 
 

Accepted Solutions (1)

Accepted Solutions (1)

david_kunz2
Product and Topic Expert
Product and Topic Expert

Hi @satya-dev ,

Looking at your provided snippet, the following seems to be wrong:

    headers.authorization = cds.context?.headers || req?.headers

I think you meant to set it to headers.authorization.

You need to make sure, that inside your remote-service handler, the authorization header is correctly set.

Also, you should await your remote service call:

await sendToSAP(req, customID)

Best regards,
David

satya-dev
Participant
0 Kudos
thanks!! David,
satya-dev
Participant
0 Kudos

thanks!! David, I was not setting auth token correctly.

const myFn = async (req, data) { cds.spawn( {  headers: req.headers.authorization, after: iafterTime }, async (req) => { await sendToSAP(req, customID) }) return 'processing in backgorund' }

Answers (0)