Wednesday 23 September 2020

Get Bearer token and pull users from SAP cloud platform

When connecting with SCP to pull the users using the API management.

Get the bearer token using the client key and secret and pass that to get the user details.

First call: To get the bearer token


POST: https://api.eu2.hana.ondemand.com/oauth2/apitoken/v1?grant_type=client_credentials


While providing authorization: you have ClientID and Secret from SCP ,so construct in the format below

Basic:  ClientID:ClientSecret – Base64 format(https://www.base64encode.net/)

Authorization : Basic NzJlYjM3NDMtOTQ3OC0zMmI4LTItMDMwM2ZkNmQyNWE0OTVhOGY0LTBlMmMtM2Y0YS05ZTRmLTVkNmY5NzIzMDJjMw== 

Output:

{

"access_token": "42d6da336c437955de45829a611f5b",

"token_type": "Bearer",

"expires_in": 1500,

"scope": "hcp.readAccountMembers hcp.manageAccountMembers hcp.manageAuthorizationSettings hcp.readAuthorizationSettings"

}


Second call: Access token to be passed as Bearer in this call and get the user details from SCP:




GET: https://api.eu1.hana.ondemand.com/authorization/v1/platform/accounts/a79a6ea13/Users



"key":"Authorization","value":"Bearer 42d6da336c437955de45829a611f5b



Output Data:


{

    "Resources": [

        {

            "id": "C5200000",

            "meta": {

                "created": "2020-09-18T13:02:33.628Z",

                "lastModified": "2020-09-18T13:02:33.628Z",

                "location": "https://apissecurity.hana.ondemand.com/authorization/v1/platform/accounts/a79a6ea10/Users/C5200000"

            },

            "schemas": [

                "urn:sap:cloud:scim:schemas:extension:custom:2.0:UserExt",

                "urn:ietf:params:scim:schemas:core:2.0:User"

            ],

            "userName": "C5200000",

            "name": {

                "familyName": "C",

                "givenName": "ramu"

            },

            "emails": [

                {

                    "value": "ramu@gmail.com",

                    "primary": true

                }

            ],

            "roles": [

                {

                    "value": "AccountAdministrator",

                    "primary": false,

                    "type": "Predefined"

                }

            ],

            "urn:sap:cloud:scim:schemas:extension:custom:2.0:UserExt": {

                "userbase": "xxCOM",

                "description": "SAP API Management team"

            }

        }





Tuesday 22 September 2020

Error: Invalid parametertype used at function 'ge'

When getting an error calling the target system as below:

Error: Invalid parametertype used at function 'ge' 


Date format to be changed from 

((CHANGED_DT ge'2020-08-10T00:00:00') and (CHANGED_DT le'2020-09-22T00:00:00'))


to


((CHANGED_DT ge datetime'2020-08-10T00:00:00') and (CHANGED_DT le datetime'2020-09-22T00:00:00'))


Hope this works!!!




Friday 18 September 2020

Script: Routing logic to send to different system based on condition in sap cpi/hci

 



Routing logic to send to different system based on condition


Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import groovy.json.*;

import groovy.util.logging.*;

import org.codehaus.*;

import groovy.xml.*;




def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String;

map = message.getProperties();




JobCategory = map.get("JobCategory");

requestid = map.get("requestid");

taskid = map.get("taskid");

message.setProperty("JobCategory1",JobCategory);

message.setProperty("requestid1",requestid);

message.setProperty("taskid1",taskid);




def route1 = "M";

def route2 = "F;

def route3 = "F;

def route4 = "M";




if(JobCategory.equals("03")) 

{

if((taskid.equals("")) && (requestid.equals("")))

{

Header = route1;

}

else

{

Header = route2;

}

}

else 

{

if((JobCategory.equals("02")) || (JobCategory.equals("01")))

{

Header = route3;

}

else

{

Header = route4;

}

}

message.setProperty("Header",Header);

return message;

}



Wednesday 16 September 2020

One line script to remove all double quotes for integer in Json data

 It is simple to remove the double quotes from the json data for the individual values. Earlier we used to pull the value in xpath and convert that value to interger to remove the double quotes.

But it makes more easier.

Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;

def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String;

// remove double quotes from all integer vaules in json data

String output = body.replaceAll("\"(\\d+)\"", "\$1");

message.setBody(output);

return message;

}



Tuesday 15 September 2020

Unmarshalling XML error in SAP CPI while converting XML to CSV convertor


Incase if you are getting this error when use XML to CSV convertor, want to add header field name in xml to csv then by standard way it's not possible. 


  1.  uncheck use field name as header in xml to csv converter.

  2. Add content modifier after your converter. In the body part add all your header field names you want like Name,Rank,subject,etc., in first line.

 3. In second line of body add ${in.body}



Thursday 10 September 2020

Groovy script to pick the count of days from today's date



import java.util.concurrent.TimeUnit;

import java.lang.*;

import java.time.*;

import com.sap.gateway.ip.core.customdev.util.Message;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;


def Message processData(Message message)

{

def map = message.getHeaders();

String getCertExpirydate = map.get("CertExpiryDate");

Date CertExpirydate = new SimpleDateFormat("yyyy-MM-dd").parse(getCertExpirydate);

Date dateNow = new Date(System.currentTimeMillis());

long dateDiff = CertExpirydate.getTime() - dateNow.getTime();

def daysToExpire = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);

message.setHeader("daysToExpire",daysToExpire);

return message;

}



Thursday 3 September 2020

Pick the xml node value and remove brackets

To pick the xml node value from the input XML which return as 

E.g: requestid => [345]

Now to remove the array brackets, also handled in script.

Now, requestid => 345 


Groovy Script:


import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;


def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String;

def data = new XmlSlurper().parseText(body);

def requestid = data.'**'.findAll { node -> node.name() == 'request_id' }*.text();

def taskid = data.'**'.findAll { node -> node.name() == 'task_id' }*.text();

requestid= requestid.toString().replaceAll("\\[", "");
requestid= requestid.toString().replaceAll('\\]', "");

message.setProperty("requestid",requestid);

taskid= taskid.toString().replaceAll("\\[", "");
taskid= taskid.toString().replaceAll('\\]', "");

message.setProperty("taskid",taskid);

return message;
}