cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP code calling REST service OAuth2.0 without OA2C

bhakti2
Active Participant
0 Kudos

hello. all the sample ABAP code snippets i found for calling OAuth2 rest service were with OA2C

i am on older version, i dont have the method execute_cc_flow in class cl_oauth_client. my version in srm 750 sp 20. im told tat i cant use oa2c.

so i have written below code to get the token and i am getting error http 400 "Missing Grant_type"
im looking for how to pass key and parameter , Grant_type = Client credentials while calling the method. Could you please help me with this and tell me if u think im doing right?

thanks in advance.


    data: lo_http_client type ref to if_http_client,
          lo_rest_client type ref to cl_rest_http_client,
          lo_request     type ref to if_rest_entity,
          lo_response    type ref to if_rest_entity,
          lv_url         type        string,
          lv_body        type        string.
    lv_url = 'https://xxxxx.us20.hana.ondemand.com/oauth/token'.
     cl_http_client=>create_by_url(
         exporting
           url                = lv_url
         importing
           client             = lo_http_client
         exceptions
           argument_not_found = 1
           plugin_not_active  = 2
           internal_error     = 3
           others             = 4 ).
    lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.

    data l_username type string.
    data l_password type string.
    l_username = 'sb-mdm-mdmdev!t557'.
    l_password = 'RhefQVuQe3sDdc4zg1X1ATSQF5g='.
    call method lo_http_client->authenticate
      exporting
        username = l_username
        password = l_password.
* Create REST client instance
    create object lo_rest_client exporting io_http_client = lo_http_client.
* Create request instance
    lo_request = lo_rest_client->if_rest_client~create_request_entity( ).
    lo_http_client->request->set_header_field( name = 'grant_type' value = 'client_credentials' ).
    lo_request->set_content_type( exporting iv_media_type = if_rest_media_type=>gc_appl_www_form_url_encoded ).
* HTTP Post
    lo_rest_client->if_rest_resource~post( lo_request ).
* HTTP response
    lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
    lo_response->set_content_type( exporting iv_media_type = if_rest_media_type=>gc_appl_json ).
* HTTP return status
    data(http_status)   = lo_response->get_header_field( '~status_code' ).
* HTTP JSON return string
    data(json_response) = lo_response->get_string_data( ).
View Entire Topic
former_member771070
Discoverer

Hi Bhakti,

You may try out the client credential options with POSTMAN once to confirm if its working standalone as desired or there is any issue with custom code.

Regarding the OA2C_CONFIG/OA2C_GRANT, I had come across a very nice and detailed wiki page which has step by step details of using 'Authorization code' option and steps relevant to OA2C_GRANT.

https://wiki.scn.sap.com/wiki/display/Security/Access+Google+APIs+using+the+OAuth+2.0+Client+API

This would help you.

Note -

In order to be able to use OA2C_GRANT, you would need the below authorization granted from security perspective

  • Object - S_OA2C_USE
    • PROFILE = <OAuth2.0 Client profile you have created>
    • ACTVT = 16
bhakti2
Active Participant
0 Kudos

thank you . it’s working in my system now . without oa2c . by creating sm59 destination