Skip to Content

Error calling business rules from ABAP in SCP

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:

        DATA(lo_http_destination) =
             cl_http_destination_provider=>create_by_url( '' ).

        "Available API Endpoints

        "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

    "uncomment the following line for console output; prerequisite: code snippet is implementation of if_oo_adt_classrun~main
    out->write( |response:  { lv_response }| ).
Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Best Answer
    Posted on May 20 at 08:12 AM

    Hi Sarah,

    I used the code below to invoke a rule service that has been deployed on cloud platform from ABAP. Could you please try it ? You would have to replace some parts that I've indicated in the code.

    REPORT z_test_http_call.
          get_client IMPORTING VALUE(iv_url)           TYPE string
                               VALUE(iv_method)        TYPE string
                               VALUE(it_header_fields) TYPE tihttpnvp
                               VALUE(iv_content_type)  TYPE string
                               VALUE(iv_request_body)  TYPE string
                     RETURNING VALUE(ro_client)        TYPE REF TO if_http_client,
          send_receive IMPORTING VALUE(io_client)   TYPE REF TO if_http_client
                       RETURNING VALUE(rv_response) TYPE string.
    CLASS lcl_invoke_cp IMPLEMENTATION.
      METHOD get_client.
        cl_http_client=>create_by_url( EXPORTING url    = iv_url
                                       IMPORTING client = ro_client ).
        ro_client->refresh_request( ).
        ro_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
        ro_client->propertytype_logon_popup = 0.
        "Set Method
        ro_client->request->set_method( iv_method ).
        "Set headers
        ro_client->request->set_header_fields( it_header_fields ).
        DATA(lo_request) = cl_rest_message_builder=>create_http_message_entity( ro_client->request ).
        lo_request->set_content_type( iv_media_type = iv_content_type ).
        lo_request->set_string_data( iv_request_body ).
        "Accepted content type
        ro_client->request->set_header_field( name  = if_http_header_fields=>accept
                                              value = cl_fdt_cp_http_client=>gc_content_type-appl_json_utf ).
      METHOD send_receive.
        io_client->send( EXCEPTIONS http_communication_failure = 1
                                       http_invalid_state         = 2
                                       http_processing_failed     = 3
                                       http_invalid_timeout       = 4 ).
        IF sy-subrc NE 0.
          WRITE 'Send Error'.
    *   Receive
        io_client->receive( EXCEPTIONS http_communication_failure = 1
                                            http_invalid_state         = 2
                                            http_processing_failed     = 3 ).
        IF sy-subrc NE 0.
          WRITE 'Receive Error'.
        rv_response = io_client->response->get_cdata( ).
      METHOD main.
        DATA : BEGIN OF ls_token,
                 access_token TYPE string,
               END OF ls_token.
        DATA(lo_client) = get_client( iv_url = '<Auth End Pt>/oauth/token' "<AuthorizationEndPt>/oauth/token
                                      iv_method = if_http_entity=>co_request_method_post "Post
                                      it_header_fields = VALUE #( ( name = if_http_header_fields=>content_type value = if_rest_media_type=>gc_appl_www_form_url_encoded  ) "Header fields
                                                                  ( name = if_http_header_fields=>connection value = 'Close' ) )
                                      iv_content_type = if_rest_media_type=>gc_appl_www_form_url_encoded
                                      iv_request_body = 'grant_type=client_credentials&client_id=<Client ID>&client_secret=<ClientSecret>' ).
        DATA(lv_resp) = send_receive( lo_client ).
        "get Access token
        /ui2/cl_json=>deserialize( EXPORTING json = lv_resp
                                   CHANGING data = ls_token ).
        IF ls_token-access_token IS NOT INITIAL.
          clear : lv_resp.
          lo_client = get_client( iv_url = '<Runtime URL>/rules-service/rest/v2/workingset-rule-services' "Endpoint of Runtime URL
                                            iv_method = if_http_entity=>co_request_method_post "Post
                                            it_header_fields = VALUE #( ( name = if_http_header_fields=>content_type value = if_rest_media_type=>gc_appl_json  ) "Header fields
                                                                        ( name = if_http_header_fields=>connection value = 'Close' )
                                                                        ( name = if_http_header_fields=>authorization value = |Bearer { ls_token-access_token }| ) )
                                            iv_content_type = if_rest_media_type=>gc_appl_json
                                            iv_request_body = '{"RuleServiceId":"<Ruleservice ID>","Vocabulary":[{<VocabularyJSON>}]}' ).
          lv_resp = send_receive( lo_client ).
          cl_demo_output=>display( lv_resp ).
      lcl_invoke_cp=>main( ).
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 14 at 07:41 AM

    Hello Sarah,
    You can take hints from these blogs (blog1, blog2) and you apply the same for rules service:

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.