Skip to Content
0
May 13, 2020 at 07:36 PM

Error calling business rules from ABAP in SCP

708 Views Last edit May 13, 2020 at 07:40 PM 2 rev

We are unable to call the Business Rules API from ABAP in SAP Cloud Platform. Please note - we are able to call it without an issue from the API Business Hub using our client id, secret and token URL.

Here is the error we get when running it from ABAP (in Eclipse):

response: {"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

Here is our source code:

 TRY.
        DATA(lo_http_destination) =
             cl_http_destination_provider=>create_by_url( 'https://bpmruleruntime.cfapps.us10.hana.ondemand.com/rules-service/rest/v2/workingset-rule-services' ).

        "Available API Endpoints
        "https://{runtimeurl}/rules-service/rest


        "create HTTP client by destination
        DATA(lo_web_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_http_destination ) .


        "adding headers with API Key for API Sandbox
        DATA(lo_web_http_request) = lo_web_http_client->get_http_request( ).
        lo_web_http_request->set_header_fields( VALUE #(
        (  name = 'Content-Type' value = 'application/json' )
        (  name = 'Accept' value = 'application/json' )
        (  name = 'APIKey' value = 'Our API key from Business Hub' )
         ) ).


        "Available Security Schemes for productive API Endpoints
        "OAuth 2.0
        lo_web_http_request->set_text('{"RuleServiceId":"Our rule service ID","Vocabulary":[{"ProfitCenterCompanyCode":{"BUKRS":"testCC","PRCTR":"ourPR"}}]}').


        "set request method and execute request
        DATA(lo_web_http_response) = lo_web_http_client->execute( if_web_http_client=>post ).
        DATA(lv_response) = lo_web_http_response->get_text( ).


      CATCH cx_http_dest_provider_error cx_web_http_client_error cx_web_message_error.
        "error handling
    ENDTRY.


    "uncomment the following line for console output; prerequisite: code snippet is implementation of if_oo_adt_classrun~main
    out->write( |response:  { lv_response }| ).
  ENDMETHOD.