Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Token Based Authentication through ABAP progarm

Former Member
21,602

Is it possible to call token based authentication using ABAP program? Please share if there's a sample code.

Thanks,

Shenal

Is it possible to call token based authentication using ABAP program? Please share if there's a sample code.

Thanks,

Shenal

4 REPLIES 4
Read only

rudramani
Participant
10,825

Hi Shenal,

Yes, it is possible. For every Https call, there will be two calls: one to get the token and another to do CRUD operations based on that token.

Sample Code to get Token:

  METHOD get_token.

    DATA: lo_http_client TYPE REF TO if_http_client.

    DATA: response TYPE string,

          lv_url   TYPE string.

    CONSTANTS: lv_initial_url TYPE string VALUE '<Your API Link>',

               lv_auth        TYPE string VALUE 'Basic <Your Authorization Data>'.





    "create HTTP client by url

    CALL METHOD cl_http_client=>create_by_url

      EXPORTING

        url                = lv_initial_url

      IMPORTING

        client             = lo_http_client

      EXCEPTIONS

        argument_not_found = 1

        plugin_not_active  = 2

        internal_error     = 3

        OTHERS             = 4.

    IF sy-subrc <> 0.

      "error handling

    ENDIF.



    "setting request method

    lo_http_client->request->set_method('GET').


    "adding headers

    lo_http_client->request->set_header_field( name = 'Authorization' value = lv_auth ).


    "Available Security Schemes for productive API Endpoints

    "OAuth 2.0

    CALL METHOD lo_http_client->send

      EXCEPTIONS

        http_communication_failure = 1

        http_invalid_state         = 2

        http_processing_failed     = 3

        http_invalid_timeout       = 4

        OTHERS                     = 5.

    IF sy-subrc = 0.

      CALL METHOD lo_http_client->receive

        EXCEPTIONS

          http_communication_failure = 1

          http_invalid_state         = 2

          http_processing_failed     = 3

          OTHERS                     = 5.

    ENDIF.

    IF sy-subrc <> 0.

      "error handling

    ENDIF.

    response = lo_http_client->response->get_cdata( ).

   GV_TOKEN = response. "Saving the Token in a Global Variable

  ENDMETHOD.

Sample Code to Fetch the token from above response and perform a Read call:

  METHOD get_proof_history.

    DATA: lo_http_client TYPE REF TO if_http_client.

    DATA: response TYPE string,

          lv_url   TYPE string,

          lv_auth  TYPE string,

          lv_auth2 TYPE string.


    CONSTANTS : lv_initial_url TYPE string VALUE '<Your API>'.



    IF iv_object_id IS NOT INITIAL.


*** Getting Token

      TYPES:

        BEGIN OF t_entry,

          access_token TYPE string,

          token_type   TYPE string,

          expires_in   TYPE n LENGTH 8,

          scope        TYPE string,

          jti          TYPE string,

        END OF t_entry .

      TYPES:

        t_entry_map TYPE SORTED TABLE OF t_entry WITH UNIQUE KEY access_token.

      DATA: m_entries TYPE t_entry.


      DATA: lr_instance  TYPE REF TO  /ui5/cl_json_parser.

      CREATE OBJECT lr_instance.

      CALL METHOD me->get_token.

      IF gv_token IS NOT INITIAL. "Retrieving your Token here

        /ui2/cl_json=>deserialize(

        EXPORTING json = gv_token pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = m_entries ).

        lv_auth2 = m_entries-access_token.

        gv_token = gv_token+17.

        CONCATENATE 'Bearer' lv_auth2 INTO lv_auth SEPARATED BY space. "Using Token Value create your Authorization Code
      ENDIF.

      DATA lv_object_id TYPE string.

      lv_object_id = iv_object_id.

      TRANSLATE lv_object_id TO LOWER CASE.

      CONCATENATE lv_initial_url lv_object_id INTO lv_url. "Appending Fix URL and the Object ID to get the Request URL

      "create HTTP client by url

      CALL METHOD 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.

      IF sy-subrc <> 0.

        "error handling

      ENDIF.


      "setting request method

      lo_http_client->request->set_method('GET').

      "adding headers

      lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).

      lo_http_client->request->set_header_field( name = 'Authorization' value = lv_auth ).

      "Available Security Schemes for productive API Endpoints

      "OAuth 2.0

      CALL METHOD lo_http_client->send

        EXCEPTIONS

          http_communication_failure = 1

          http_invalid_state         = 2

          http_processing_failed     = 3

          http_invalid_timeout       = 4

          OTHERS                     = 5.

      IF sy-subrc = 0.

        CALL METHOD lo_http_client->receive

          EXCEPTIONS

            http_communication_failure = 1

            http_invalid_state         = 2

            http_processing_failed     = 3

            OTHERS                     = 5.

      ENDIF.


      IF sy-subrc = 1.

        "error handling

        ev_response = 'http_communication_failure'.

      ELSEIF sy-subrc = 2.

        ev_response = 'http_invalid_state'.

      ELSEIF sy-subrc = 3.

        ev_response = 'http_processing_failed'.

      ELSEIF sy-subrc = 0.

        response = lo_http_client->response->get_cdata( ).

*WRITE: 'response: ', response.

        ev_response = response.

      ELSE.

        ev_response = 'Unknown Error'.

      ENDIF.

    ENDIF.

  ENDMETHOD.
Read only

0 Likes
10,825

How and from where do you get the value for iv_object_id?

Can you post the clear and complete code ?

Read only

0 Likes
10,825

shaik.zia iv_object_id is not important here, what is important is "Basic", "Authorization" and "Bearer" parts. It's an old post so better ask your own question with all details.

Read only

raya_hemanth
Explorer
0 Likes
10,825

Hi Rudramani Pandey,

How do we set header content length? I am getting error message "HTTP Error 411. The request must be chunked or have a content length.".

Thanks,

~Hemanth