<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Token Based Authentication through ABAP progarm in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008628#M1967622</link>
    <description>&lt;P&gt;How and from where do you get the value for &lt;STRONG&gt;iv_object_id&lt;/STRONG&gt;? &lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Can you post the clear and complete code ? &lt;/P&gt;</description>
    <pubDate>Wed, 18 Oct 2023 10:57:53 GMT</pubDate>
    <dc:creator>shaik_zia</dc:creator>
    <dc:date>2023-10-18T10:57:53Z</dc:date>
    <item>
      <title>Token Based Authentication through ABAP progarm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008625#M1967619</link>
      <description>&lt;P&gt;Is it possible to call token based authentication using ABAP program? Please share if there's a sample code.&lt;/P&gt;
  &lt;P&gt;Thanks,&lt;/P&gt;
  &lt;P&gt;Shenal&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jul 2019 04:52:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008625#M1967619</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2019-07-25T04:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: Token Based Authentication through ABAP progarm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008626#M1967620</link>
      <description>&lt;P&gt;Hi Shenal,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Sample Code to get Token:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;  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 '&amp;lt;Your API Link&amp;gt;',

               lv_auth        TYPE string VALUE 'Basic &amp;lt;Your Authorization Data&amp;gt;'.





    "create HTTP client by url

    CALL METHOD cl_http_client=&amp;gt;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 &amp;lt;&amp;gt; 0.

      "error handling

    ENDIF.



    "setting request method

    lo_http_client-&amp;gt;request-&amp;gt;set_method('GET').


    "adding headers

    lo_http_client-&amp;gt;request-&amp;gt;set_header_field( name = 'Authorization' value = lv_auth ).


    "Available Security Schemes for productive API Endpoints

    "OAuth 2.0

    CALL METHOD lo_http_client-&amp;gt;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-&amp;gt;receive

        EXCEPTIONS

          http_communication_failure = 1

          http_invalid_state         = 2

          http_processing_failed     = 3

          OTHERS                     = 5.

    ENDIF.

    IF sy-subrc &amp;lt;&amp;gt; 0.

      "error handling

    ENDIF.

    response = lo_http_client-&amp;gt;response-&amp;gt;get_cdata( ).

   GV_TOKEN = response. "Saving the Token in a Global Variable

  ENDMETHOD.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Sample Code to Fetch the token from above response and perform a Read call:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;  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 '&amp;lt;Your API&amp;gt;'.



    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-&amp;gt;get_token.

      IF gv_token IS NOT INITIAL. "Retrieving your Token here

        /ui2/cl_json=&amp;gt;deserialize(

        EXPORTING json = gv_token pretty_name = /ui2/cl_json=&amp;gt;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=&amp;gt;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 &amp;lt;&amp;gt; 0.

        "error handling

      ENDIF.


      "setting request method

      lo_http_client-&amp;gt;request-&amp;gt;set_method('GET').

      "adding headers

      lo_http_client-&amp;gt;request-&amp;gt;set_header_field( name = 'Accept' value = 'application/json' ).

      lo_http_client-&amp;gt;request-&amp;gt;set_header_field( name = 'Authorization' value = lv_auth ).

      "Available Security Schemes for productive API Endpoints

      "OAuth 2.0

      CALL METHOD lo_http_client-&amp;gt;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-&amp;gt;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-&amp;gt;response-&amp;gt;get_cdata( ).

*WRITE: 'response: ', response.

        ev_response = response.

      ELSE.

        ev_response = 'Unknown Error'.

      ENDIF.

    ENDIF.

  ENDMETHOD.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jul 2019 11:16:50 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008626#M1967620</guid>
      <dc:creator>rudramani</dc:creator>
      <dc:date>2019-07-25T11:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Token Based Authentication through ABAP progarm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008627#M1967621</link>
      <description>&lt;P&gt;Hi Rudramani Pandey,&lt;/P&gt;&lt;P&gt;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.".&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;~Hemanth&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 05:37:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008627#M1967621</guid>
      <dc:creator>raya_hemanth</dc:creator>
      <dc:date>2022-04-07T05:37:34Z</dc:date>
    </item>
    <item>
      <title>Re: Token Based Authentication through ABAP progarm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008628#M1967622</link>
      <description>&lt;P&gt;How and from where do you get the value for &lt;STRONG&gt;iv_object_id&lt;/STRONG&gt;? &lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Can you post the clear and complete code ? &lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2023 10:57:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008628#M1967622</guid>
      <dc:creator>shaik_zia</dc:creator>
      <dc:date>2023-10-18T10:57:53Z</dc:date>
    </item>
    <item>
      <title>Re: Token Based Authentication through ABAP progarm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008629#M1967623</link>
      <description>&lt;P&gt;&lt;SPAN class="mention-scrubbed"&gt;shaik.zia&lt;/SPAN&gt; 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.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2023 15:26:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/token-based-authentication-through-abap-progarm/m-p/12008629#M1967623</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2023-10-18T15:26:59Z</dc:date>
    </item>
  </channel>
</rss>

