How to consume S4 HANA Cloud API in SAP BTP ABAP environment by using certificate-based authentication
We can consume APIs in BTP Extension suite with different types of authorization mechanisms.
In this example I am explaining how to consume S4HC API (Business partner Data) in SAP BTP by using RAP Model. Please note, This will not work in Developer extensibility framework.
Our focus will be using the certificate-based authentication model.
As per our scenario, basic authentication is not recommended due to organization policies so need to implement certificate based.
How it Works ?
Below Steps can be followed ,
Step1 : Download Trust Certificate from BTP Cockpit
- Open subaccount containing ABAP system in the Cloud Foundry environment.
- In the SAP BTP cockpit, Connectivity Destinations needs to be Chosen and then Download Trust
- Save the trust certificate locally .
Step 2: Create Communication Arrangement in SAP S/4HANA Cloud
- In the launchpad, open the Communication Arrangements tile and choose new.
- Select communication scenario SAP_COM_0008 (Business Partner)
- SAP_COM_0008 is the communication scenario that we need to set up communication with SAP BTP as business partner.
- Enter an arrangement name, for example, ZEXT_0008_OAUTH.
- Choose Create.
Step 3: Copying the Inbound Service URL and Other Communication Details (SAP S/4HANA Cloud)
- In the launchpad, choose the Communication Arrangements tile and choose the communication arrangement that you have created.
- In the Inbound Services screen area, find the inbound service relevant to your use case (here, the ODataV2 service Business Partner Integration) and copy the host name of the service URL for later use
- In the Inbound Communication screen area, choose OAuth 2.0 Details.
- From the following popup, copy the client ID, username, token service URL, and SAML2 audience for later use.
Step 4: Creating the SAMLAssertion Destination to SAP S/4HANA Cloud
- Log on to the SAP BTP cockpit as administrator.
- Navigate to your global account and to your ABAP subaccount.
- From the navigation area, choose Destinations and create. Use the following details as shown in SS.
- Check the connection if its successful or not
Your BTP ABAP system is ready to consume API of S4HC. Follow the next steps to consume Data in RAP Model.
Step 1: Create a service consumption Model in ADT and choose OData as Remote Consumption Mode.
Step 2: Upload the metadata of the required API and put a prefix.
Step 3: Now Check the Generated artifacts. Your service definition is ready.
Step 4: Check your service consumption model, find Entity details. In Entity details, sample code is generated for entity details access. The generated code can be used for later use .
Step 5: Create a Http handler class and access the API data with sample RAP code below.
class ZCL_TEST_HTTP definition public create public . public section. interfaces IF_HTTP_SERVICE_EXTENSION . protected section. private section. ENDCLASS. CLASS ZCL_TEST_HTTP IMPLEMENTATION. method IF_HTTP_SERVICE_EXTENSION~HANDLE_REQUEST. try. DATA: lt_business_data TYPE TABLE OF ZZ_BPA_BPRELATIONSHI329148BAE7, lo_http_client TYPE REF TO if_web_http_client, * lo_client_proxy TYPE REF TO /iwbep/if_cp_client_proxy, lo_request TYPE REF TO /iwbep/if_cp_request_read_list, lo_response TYPE REF TO /iwbep/if_cp_response_read_lst. DATA(lo_destination) = cl_http_destination_provider=>create_by_cloud_destination( i_name = 'S4C_DEV_080' i_authn_mode = if_a4c_cp_service=>user_propagation ). cl_web_http_client_manager=>create_by_http_destination( EXPORTING i_destination = lo_destination RECEIVING r_client = lo_http_client ). **Relative service path** lo_http_client->get_http_request( )->set_uri_path( '/sap/opu/odata/sap/API_BUSINESS_PARTNER' ). lo_http_client->set_csrf_token( ). DATA(lo_client_proxy) = cl_web_odata_client_factory=>create_v2_remote_proxy( iv_service_definition_name = 'ZZ_TEST0008’ io_http_client = lo_http_client iv_relative_service_root = '/sap/opu/odata/sap/API_BUSINESS_PARTNER' ). " Navigate to the resource and create a request for the read operation lo_request = lo_client_proxy->create_resource_for_entity_set( 'A_BPRELATIONSHIP' )->create_request_for_read( ). " Create the filter tree *lo_filter_factory = lo_request->create_filter_factory( ). * *lo_filter_node_1 = lo_filter_factory->create_by_range( iv_property_path = 'BUSINESSPARTNER' * it_range = lt_range_BUSINESSPARTNER ). *lo_filter_node_2 = lo_filter_factory->create_by_range( iv_property_path = 'BUSINESSPARTNERFISCALYEAR' * it_range = lt_range_BUSINESSPARTNERFISCALYEAR ). *lo_filter_node_root = lo_filter_node_1->and( lo_filter_node_2 ). *lo_request->set_filter( lo_filter_node_root ). *lo_request->set_top( 50 )->set_skip( 0 ). " Execute the request and retrieve the business data lo_response = lo_request->execute( ). lo_response->get_business_data( IMPORTING et_business_data = lt_business_data ). CATCH /iwbep/cx_cp_remote INTO data(lx_remote). " Handle remote Exception " It contains details about the problems of your http(s) connection CATCH /iwbep/cx_gateway INTO DATA(lx_gateway). " Handle Exception "CATCH cx_web_http_client_error INTO DATA(lx_web_http_client_error). " Handle Exception "RAISE SHORTDUMP lx_web_http_client_error. ENDTRY. endmethod. ENDCLASS.
This code can be used inside the select method of if_rap_query_provider also .
The codes in this example are purely for illustrative purposes. No performance testing has been done
Please feel free to comment if you want to add any point .