Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
DebashishDas
Active Participant
What: 

This blog post is about consuming external services from BTP after deploying your service/application. This is the continuation of the previous post Part- 1.

Here we are going to deploy and access the same external service from the cloud foundry

How:

Deploying Application

Now we are going to deploy our CAP project in SCP Cloud Foundry

Before deploying using the command, we need to build the project.
$ cds build/all

Create mta.yaml file by using the below command.
$ cds add mta

MTA file will look like below –
---
_schema-version: '3.1'
ID: BusinessPartner
version: 1.0.0
description: "A simple CAP project."
parameters:
enable-parallel-deployments: true
build-parameters:
before-all:
- builder: custom
commands:
- npm ci
- npx -p @sap/cds-dk cds build --production

modules:
- name: BusinessPartner-srv
type: nodejs
path: gen/srv
parameters:
buildpack: nodejs_buildpack
build-parameters:
builder: npm-ci
provides:
- name: srv-api # required by consumers of CAP services (e.g. approuter)
properties:
srv-url: ${default-url}

As this file does not hold enough information to run the application in the Cloud foundry, We need two resources binding with our service application to up and running in CF.

  1. Destination service

  2. xsuaa service


Let create the Destination service in BTP

Login to your account and search for destination under service marketplace.


Click on create Button and provide the Instance name. In my case, I have created demo-destination instance.
Service: Destination Service
Plan: lite
Runtime Environment: Cloud Foundry
Space: dev
Instance Name: demo-destination


Click on demo-destination instance and configure a new destination
URL=https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER/
Name=API_BUSINESS_PARTNER
ProxyType=Internet
Type=HTTP
Authentication=NoAuthentication
Description=Business Partner SandBox API

Now we have created one service instance for our application. Let's create xs-security.json for uaa service instance.

Run the below command
$ cds compile srv/ --to xsuaa > xs-security.json

We have to update our MTA file to make binding these two services with our application
---
_schema-version: '3.1'
ID: BusinessPartner
version: 1.0.0
description: "A simple CAP project."
parameters:
enable-parallel-deployments: true
build-parameters:
before-all:
- builder: custom
commands:
- npm ci
- npx -p @sap/cds-dk cds build --production
modules:
- name: BusinessPartner-srv
type: nodejs
path: gen/srv
parameters:
buildpack: nodejs_buildpack
build-parameters:
builder: npm-ci
provides:
- name: srv-api # required by consumers of CAP services (e.g. approuter)
properties:
srv-url: ${default-url}
requires:
- name: demo-destination
- name: businesspartner-uaa

resources:
- name: demo-destination
type: org.cloudfoundry.existing-service
parameters:
service-name: demo-destination

- name: businesspartner-uaa
type: org.cloudfoundry.managed-service
parameters:
path: ./xs-security.json
service: xsuaa
service-plan: application
config:
xsappname: businesspartner-${org}-${space}
tenant-mode: dedicated

We have to change the credentials to access the destination service in the package.json file.
"cds": {
"requires": {
"API_BUSINESS_PARTNER": {
"kind": "odata-v2",
"model": "srv/external/API_BUSINESS_PARTNER",
"[development]": {
"credentials": {
"url": "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER",
"headers": {
"APIKey": "<api-key>"
}
}
},
"[production]": {
"credentials": {
"destination": "API_BUSINESS_PARTNER"
}
}
},
"uaa": {
"kind": "xsuaa"
}
}
}

When you are going to access sandbox data for your local development you have to use "[development]" credentials.

Follow the below commands to deploy your cap project -
$ cds build/all
$ mbt build -t ./
$ cf deploy BusinessPartner_1.0.0.mtar

BusinessPartner_1.0.0.mtar, is generated after your MBT build command.

Now check in the BTP under the application section. The application has been deployed and it is has been Started.


Click on the application URL.


Click on A_BusinessPartner


Oops!! It was working fine in our BAS.

The reason is we are not passing APIkey to access the sandbox data. In package.json file we pass only destination name inside "[production]", not the apikey.

There are two ways I found how to use API key for the production environment.

  1. Set the apikey in as an environment variable and use it in your code. In every request send it in as a header request.

  2. We can modify the destination configuration in our destination service.


Here I am we are going to follow the second approach.

Open the destination service and an additional parameter

URL.headers.apiKey - <api-key>


 

Now again click on A_BusinessPartner


 

Response is coming from the API hub sandbox.

Now you know how to access API Hub sandbox data from a cap application

 

 

NOTE on 3rd December 2022: 

If you follow this blog you need to lower the version "@sap/cds": "^5" in package.json file. 

Or,

If use the latest version. follow the steps below -

Package dependencies -
  "dependencies": {
"@sap-cloud-sdk/http-client": "^2.12.0",
"@sap-cloud-sdk/util": "^2.12.0",
"@sap/cds": "^6",
"@sap/xsenv": "^3.4.0",
"@sap/xssec": "^3.2.14",
"express": "^4",
"passport": "^0.6.0"
},

 

Add authorisation on service -
service BusinessService @(requires: 'authenticated-user'){  
@cds.autoexpose
entity A_BusinessPartner as projection on abp.A_BusinessPartner {
key BusinessPartner, Customer, BusinessPartnerFullName, BusinessPartnerGrouping, BusinessPartnerUUID, OrganizationBPName1
}
}

 

Add the below parameters in xs-security.json -
{
"scopes": [],
"attributes": [],
"role-templates": [],
"authorities-inheritance": false,
"oauth2-configuration": {
"redirect-uris": [
"https://*.hana.ondemand.com/**"
]
}
}

 

Add approuter.

command - cds add approuter.

This will update the mta.yaml file and files files will be created corresponding to the approuter. A new module will be created. this is basically used for routing.
  - name: BusinessPartner
type: approuter.nodejs
path: app/ # from cds.env.folders. Consider also cds.env.build.target -> gen/app
parameters:
keep-existing-routes: true
disk-quota: 256M
memory: 256M
requires:
- name: srv-api
group: destinations
properties:
name: srv-api # must be used in xs-app.json as well
url: ~{srv-url}
forwardAuthToken: true
- name: BusinessPartner-xsuaa


 

Build and deploy the latest changes. Now one new Application will be created in your dev space. Execute "BusinessPartner". This will retrieve all the data like before.


 

 
10 Comments
0 Kudos
Hello,

 

Thank you for the tutorial. I have a little problem in the last step because when I deploy my app and I test it the server send me a 401 Unauthorized error. I think the XSUAA doesn't work properly but I'm not sure. Could you help me?

 

Thank you very much,

Ismael
DebashishDas
Active Participant
0 Kudos

Hello imuela_orange11

Please validate your configuration in BTP destination service and apikey. 

VijayVegesana1
Explorer
0 Kudos
Hi Debashish,

Even am receiving the same 401 - unauthorized error after deployment. Have crossed checked all the configurations multiple times, could you pls share any insights here ?

Locally it is working fine with apiKey.

Reproduce the issue:

  1. Access the Cloud Foundry URL

  2. Click on A_BusinessPartner => 401 unauthorized error is displaying


Regards,

Maq
VijayVegesana1
Explorer
0 Kudos
Hi Ismael,

 

Were you able to fix the issue ?

 

Thanks..
DebashishDas
Active Participant
0 Kudos

Hi Maqsood,

Do changes in your mta.yml file. Delete the previous destination resource.

resources:
- name: BusinessPartner-destination
type: org.cloudfoundry.managed-service
parameters:
service: destination
service-plan: lite

This will create a BusinessParnter-destination service instance. Later you have to create a  New Destination same as before process.

 

 

 

 

VijayVegesana1
Explorer
0 Kudos

Hi Debashish,

Thanks for the detailed response.

I tried all the mentioned options but no luck, issue remains same. Are there any other steps to cross check ? this looks to be little strange.

  1. Existing destination instance is deleted
  2. Added the destination as you mentioned
  3. Redeployed
  4. Maintained the destination "API_BUSINESS_PARTNER"

 

 

 

VijayVegesana1
Explorer
0 Kudos

Added the mta yaml screenshot



API BUSINESS PARTNER:


 
DebashishDas
Active Participant
Hi Maqsood,

You have to lower the version of “@sap/cds”: “^5” in package.json file.

Run npm install > Build > deploy.

 
VijayVegesana1
Explorer
0 Kudos
Hi Debashish,

Thank you for the responses, we have added the approuter in the current version and deployed, this is working fine now via the approuter app.

command line: cds add approuter

Thank you for the help.

 

 

 
tomjohn_br
Explorer
0 Kudos
It solved my problem as well! Thanks a lot for your support!

 
Labels in this area