cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Cloud SDK for Javascript: Validation of Destination object returned by Destination Service fails

former_member182330
Active Contributor
0 Kudos

Hello Experts,

I'm starting my journey in learning BTP development / JS / TS and SAP Cloud SDK for Javascript.

I'm trying to call API business hub HTTP service that is defined as Destination in the subaccount. It appears like the Destination object is properly retrieved by @sap/destinations lib. But when I attempt to call the API via generated open API library, validation of passed Destination object fails. Could someone give me a hint what is wrong? It appears like SDK validation looks for lowercase object property url, while Destination object includes uppercase URL property. Is this a bug in Javascript SDK?

Regards,

Konstantin

import { Controller, Get } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { any, Destination, DestinationNameAndJwt } from '@sap-cloud-sdk/core';
import { AppService } from './app.service';
import * as xsenv from '@sap/xsenv';
//const xsenv = require('@sap/xsenv');
xsenv.loadEnv();
import * as destinationsClient from '@sap/destinations';
//const  destinationsClient = require('@sap/destinations');
import { MaterialApi } from './dmcapi/material';
import { materialDestination } from './materialDestination';

@Controller()
export class AppController {

  constructor(private readonly appService: AppService,
    private config: ConfigService) { }

  @Get('material')
  material(): string {
    console.log("Executing material() @ Controller");
    const destServiceDetails = xsenv.getServices({
      destination: { tag: 'destination' }
    });

    const destClient = destinationsClient(destServiceDetails);
    destClient.load("DMC_API_Material").then((materialApiDest) => {
      console.log(materialApiDest);
      console.log(MaterialApi.findMaterialByPlantAndNameUsingGet1({material: "MAT_1", plant: "UV10", version: "ERP001"}).execute(materialApiDest));
    }).catch((err) => {
      console.log(err);
    })
    return "done!";
  }
}

Exception:

 2021-06-17T22:24:24.46-0400 [APP/PROC/WEB/0] OUT {"message":"The custom headers are provided with the keys: . These keys will overwrite the headers created by the SDK.","level":"warn","custom_fields":{"package":"core","messageContext":"http-client"},"logger":"sap-cloud-sdk-logger","timestamp":"2021-06-18T02:24:24.468Z","msg":"The custom headers are provided with the keys: . These keys will overwrite the headers created by the SDK.","written_ts":1623983064468,"written_at":"2021-06-18T02:24:24.468Z"}
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] OUT Promise { <pending> }
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR (node:151) UnhandledPromiseRejectionWarning: ErrorWithCause: Failed to build HTTP request for destination: failed to load destination!
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at new ErrorWithCause (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/util/dist/error-with-cause.js:32:16)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at /home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/http-client/http-client.js:208:15
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at process._tickCallback (internal/process/next_tick.js:68:7)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR Caused by:
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR Error: Property 'url' of destination input must not be undefined.
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at validateDestinationInput (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/destination/destination.js:60:15)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at Object.sanitizeDestination (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/destination/destination.js:13:5)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at Object.<anonymous> (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/destination/destination-accessor.js:76:37)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at step (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/destination/destination-accessor.js:44:23)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at Object.next (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/destination/destination-accessor.js:25:53)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at /home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/destination/destination-accessor.js:19:71
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at new Promise (<anonymous>)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at __awaiter (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/destination/destination-accessor.js:15:12)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at Object.useOrFetchDestination (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/destination/destination-accessor.js:72:12)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR     at resolveDestination (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/http-client/http-client.js:207:21)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR (node:151) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
   2021-06-17T22:24:24.47-0400 [APP/PROC/WEB/0] ERR (node:151) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The destination object logged by the code and passed to execute() method:

{
      owner:
       { SubaccountId: '027eb2....',
         InstanceId: null },
      destinationConfiguration:
       { Name: 'DMC_API',
         Type: 'HTTP',
         URL:
          'https://dm-prod-prod-fnd-api-gateway-ms.cfapps.eu10.hana.ondemand.com',
         Authentication: 'OAuth2ClientCredentials',
         ProxyType: 'Internet',
         Description: 'SAP DMC Default Destination',
         clientId:
          'sb-59cc2ebb-544......',
         tokenServiceURL:
          'https://my-tenant.authentication.eu10.hana.ondemand.com/oauth/token',
         clientSecret: 'i5jG....' },
      authTokens:
       [ { type: 'bearer',
           value:
            'eyJhbGciOiJ......',
           http_header: [Object],
           expires_in: '43040',
           scope:
            'dmc-services!b1334.u dmc-services!b1334.am.i' } ] 
}

gregorw
Active Contributor
0 Kudos

Don't get why an SAP Empoyee is using nestjs and not SAP Cloud Application Programming Model.

former_member182330
Active Contributor
0 Kudos

gregorw It's part of SAP Cloud SDK for JS. What's wrong with that?

gregorw
Active Contributor
0 Kudos

Because you would create OData V4 Services when using CAP.

Junjie
Advisor
Advisor
0 Kudos

Hi konstantin.chernega ,

I checked your code and it seems you have used the SAP Cloud SDK for generating an OpenAPI client. What you would like to do now it to execute a request called "MaterialApi.findMaterialByPlantAndNameUsingGet1" with a given destination.

To achieve so, you just need to do like the example below:

MaterialApi.findMaterialByPlantAndNameUsingGet1({material: "MAT_1", plant: "UV10", version: "ERP001"}).execute('destinationName'));

This means, you just need to provide the name of the destination and please do not provide an object built from other libs like "@sap/destinations", which is not compatible with the SAP Cloud SDK.

Please check our documentation here for more details about how to execute an OpenAPI request .

For destination service, please find the docs here.

Btw, the SDK shows some examples with nestjs, but it's not part of the SDK.

Please create an issue here as our official support channel, in case you still have questions.

Best regards,

Junjie

former_member182330
Active Contributor
0 Kudos
 dsfas Does not seem like execute() can accept a string. I tried as follows:
MaterialApi.findMaterialByPlantAndNameUsingGet1({material: "D2O_DMC_CONTROL_HEAD_BLUE", plant: "UV10", version: "ERP001"}).execute({destinationName:'DMC_API_Material'});

.. but received an error:

   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR [Nest] 151   - 06/18/2021, 1:47:53 PM   [ExceptionsHandler] Failed to build HTTP request for destination: failed to build headers! +28618ms
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR ErrorWithCause: Failed to build HTTP request for destination: failed to build headers!
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR     at new ErrorWithCause (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/util/dist/error-with-cause.js:32:16)
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR     at /home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/http-client/http-client.js:203:15
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR     at process._tickCallback (internal/process/next_tick.js:68:7)
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR Caused by:
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR Error: The destination tried to provide authorization tokens but failed in all cases. This is most likely due to misconfiguration.
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR Original error messages:
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR Could not verify the provided CSRF token because your session was not found.
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR     at headerFromTokens (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/authorization-header.js:152:15)
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR     at /home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/authorization-header.js:222:43
   2021-06-18T09:47:53.07-0400 [APP/PROC/WEB/0] ERR     at step (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/authorization-header.js:44:23)

former_member182330
Active Contributor
0 Kudos

gregorw that was my problem with CAP. I didn't need OData service or a DB data model in my app.

Accepted Solutions (0)

Answers (1)

Answers (1)

gregorw
Active Contributor

Have you tried to simply just call:

sdkDestination = await getDestination(destinationName);

like I do in https://github.com/jowavp/sap-cf-proxy/blob/main/ts/proxy/index.ts#L89. When running locally I need to add:

xsenv.loadEnv();

But I think you can get rid of so much code.

former_member182330
Active Contributor
0 Kudos

Looks like it returns slightly different object than @sap/destinations lib. And now I get another exception in .execute(materialApiDest)

Same as in my response to Junjie Tang

   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR [Nest] 152   - 06/18/2021, 2:20:08 PM   [ExceptionsHandler] Failed to build HTTP request for destination: failed to build headers! +14029ms
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR ErrorWithCause: Failed to build HTTP request for destination: failed to build headers!
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR     at new ErrorWithCause (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/util/dist/error-with-cause.js:32:16)
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR     at /home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/http-client/http-client.js:203:15
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR     at process._tickCallback (internal/process/next_tick.js:68:7)
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR Caused by:
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR Error: The destination tried to provide authorization tokens but failed in all cases. This is most likely due to misconfiguration.
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR Original error messages:
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR Could not verify the provided CSRF token because your session was not found.
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR     at headerFromTokens (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/authorization-header.js:152:15)
   2021-06-18T10:20:08.54-0400 [APP/PROC/WEB/0] ERR     at /home/vcap/deps/0/node_modules/@sap-cloud-sdk/core/dist/connectivity/scp-cf/authorization-header.js:222:43