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: 
kallolathome
Active Participant
7,060

Introduction


I've checked blog-posts for accessing the destinations having authentication OAuth2ClientCredentials in Cloud Foundry via Node.js . But I didn't find any code working properly.

Then I came across iobert's blog post about Using the Destination service in SAP BTP, Cloud Foundry Environment. The blog-post is really very nice and helpful. But there was one problem. The library: request used is deprecated.

So, thought of rewriting the code using the axios library. The sample code snippet is provided below for reference.

Pre-requisites:  Please bind your application with the instances of Destination Service & Authorization and Trust Management Service respectively.

Solution


// imports
const express = require("express");
const cfenv = require('cfenv');
const axios = require("axios");

const app = express();
const PORT = process.env.PORT || 4000;

const getProjectDef = async (req, res) => {

const MAX_ROWS = "1000";

// Get the UAA and destination services
const UAA_SERVICE = cfenv.getAppEnv().getService('UAA_INSTANCE_NAME');
const DESTINATION_SERVICE = cfenv.getAppEnv().getService('DESTINATION_SERVICE_NAME');

// Combine the client ID and secret for the UAA service into a single string
const UAA_CREDENTIALS = DESTINATION_SERVICE.credentials.clientid + ':' + DESTINATION_SERVICE.credentials.clientsecret;

// Set the name of the destination to retrieve and the endpoint to call
const DESTINATION_NAME = 'DESTINATION_NAME';
const END_POINT = 'END_POINT_URL';

// Set the payload for the POST request to the UAA to get a token
const post_payload = {
'client_id': DESTINATION_SERVICE.credentials.clientid,
'grant_type': 'client_credentials'
};

// Set the configuration options for the POST request to the UAA
const post_config = {
method: 'POST',
url: UAA_SERVICE.credentials.url + '/oauth/token',
headers: {
'Authorization': 'Basic ' + Buffer.from(UAA_CREDENTIALS).toString('base64'), // Encode the client ID and secret as base64
'Content-type': 'application/x-www-form-urlencoded'
},
data: new URLSearchParams(post_payload).toString() // Encode the payload as x-www-form-urlencoded
};

// Make the POST request to the UAA to get a token
axios(post_config)
.then((response) => {
if (response.status === 200) {
const token = response.data.access_token; // Get the token from the response data
// Set the configuration options for the GET request to the destination service
const get_config = {
method: 'GET',
url: DESTINATION_SERVICE.credentials.uri + '/destination-configuration/v1/destinations/' + DESTINATION_NAME,
headers: {
'Authorization': 'Bearer ' + token // Include the token in the authorization header of the GET request
}
};

// Make the GET request to the destination service to retrieve the destination configuration
axios(get_config)
.then((response) => {
const DESTINATION = response.data; // Get the destination configuration from the response data

// Get the auth token from the destination configuration
const token = DESTINATION.authTokens[0];

// Set the configuration options for the POST request to the endpoint of the destination service
// a CPI is built over the BAPI: BAPI_PROJECTDEF_GETLIST with OAuth2ClientCredentials authentication
const options = {
method: 'POST',
url: DESTINATION.destinationConfiguration.URL + END_POINT,
headers: {
'Authorization': `${token.type} ${token.value}` // Include the auth token in the authorization header of the GET request
},
data: {
MaxRows: MAX_ROWS,
ProjectDefinitionRange: {
item: [
{
Sign: req.data.sign,
Option: req.data.option,
Low: req.data.low,
High: req.data.high,
},
],
},
}
};

// Make the GET request to the endpoint of the destination service
axios(options)
.then((response) => {
res.send(response.data); // Return data
})
.catch((error) => {
res.send(error); // Return error
});
})
.catch((error) => {
res.send(error); // Return error
});
}
})
.catch((error) => {
res.send(error); // Return error
});
console.log('Exit :------->');
};

// endpoint
app.get("/getProjectDef", getProjectDef);

app.listen(PORT),
() => {
console.log(`Listnening to the PORT: ${PORT}`);
};

 

N.B: This code snippet can be easily used in the CAPM(Node.js) applications. There is another library named sap-cf-axios which can be used if you want to code less.

 

Happy Coding!
6 Comments
Labels in this area