cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to call rest api using bearer token

vikash_pathak
Participant
0 Likes
5,729

Hello,

I have a requirement where i am accessing two API'S , first API return Bearer Token and for accessing Second API for authentication instead user id and password the bearer token will be used which is received from First API call

Let me know how to call API with Bearer Token.

Thanks

Vikash

Accepted Solutions (0)

Answers (1)

Answers (1)

sergei-u-niq
Active Contributor
0 Likes

just add Header

Authorization: Bearer xxxxxxxTOKENxxxx
vikash_pathak
Participant
0 Likes

Where do i need to add this authorization?

i have written this code

data: lo_request1 type ref to if_rest_entity.
*      lo_rest_client type ref to cl_rest_http_client.
CLEAR:LV_GITHIB_url .




LV_GITHIB_url = 'https://xxxxxxxxxxxxxxxxx'.
cl_http_client=>create_by_url(
      EXPORTING
        url                = LV_GITHIB_url
        ssl_id             = 'ANONYM'
      IMPORTING
        client             = http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4 ).
    IF sy-subrc <> 0.
      CLEAR ev_response.
      RETURN.
    ENDIF.
DATA(lo_rest_client) = new cl_rest_http_client( IO_HTTP_CLIENT = http_client ).
http_client->propertytype_logon_popup = http_client->co_disabled.
lo_request1 = lo_rest_client->if_rest_client~create_request_entity( ).
lo_request1->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
lo_request1->set_header_field( iv_name  = 'X-CSRF-Token' iv_value = ev_reason1 ).
lo_request1->set_header_field( iv_name = 'X-Requested-With' iv_value = 'X' ).
cl_http_utility=>set_request_uri( request = http_client->request
                                uri    = LV_GITHIB_url ).


  lo_request1->set_string_data( wf_string1 ).


  lo_rest_client->if_rest_resource~post( lo_request1 ).


  lo_response = lo_rest_client->if_rest_client~get_response_entity( ).


  lv_http_status = lo_response->get_header_field( '~status_code' ).


  lv_response = lo_response->get_string_data( ).


  write:/ lv_http_status.
  write:/ lv_response.
sergei-u-niq
Active Contributor
0 Likes

use

lo_request->set_header_field( iv_name='Authorization' iv_value = |Bearer { lv_token }| )

on your second call

vikash_pathak
Participant
0 Likes

Hi Sergei Haller,

i tried with it , but getting 401 unauthorized error .

Thanks

sergei-u-niq
Active Contributor
0 Likes

Well, thats how a bearer token is transported. Does it work in Postman or other tools? is your token correct?

vikash_pathak
Participant
0 Likes

Hello sergei.haller.u-niq,

yes i am using the correct Token and i tested it on postman and its working there,

if i donot use http_client->propertytype_logon_popup = http_client->co_disabled. this code login screen popup for user id nd password even after using below code.

LV_GITHIB_url = 'https://xxxxxxxxxxxxxxxxxxxxxxxx'.
cl_http_client=>create_by_url(
      EXPORTING
        url                = LV_GITHIB_url
        ssl_id             = 'ANONYM'
      IMPORTING
        client             = http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4 ).
    IF sy-subrc <> 0.
      CLEAR ev_response.
      RETURN.
    ENDIF.
DATA(lo_rest_client) = new cl_rest_http_client( IO_HTTP_CLIENT = http_client ).
lo_request1 = lo_rest_client->if_rest_client~create_request_entity( ).
*http_client->propertytype_logon_popup = http_client->co_disabled.
lo_request1->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
lo_request1->set_header_field( iv_name = 'Authorization' iv_value = |Bearer { ev_reason1 }| ).
lo_request1->set_header_field( iv_name  = 'X-CSRF-Token' iv_value = ev_reason1 ).
lo_request1->set_header_field( iv_name = 'X-Requested-With' iv_value = 'X' ).
cl_http_utility=>set_request_uri( request = http_client->request
                                uri    = LV_GITHIB_url ).




  lo_request1->set_string_data( wf_string1 ).


  lo_rest_client->if_rest_resource~post( lo_request1 ).


  lo_response = lo_rest_client->if_rest_client~get_response_entity( ).


  lv_http_status = lo_response->get_header_field( '~status_code' ).


  lv_response = lo_response->get_string_data( ).

sergei-u-niq
Active Contributor
0 Likes

you are passing ev_reason1 as CSRF token and as Bearer Token. It cant be both. Which one is it?

vikash_pathak
Participant
0 Likes

Hello Sergei Haller,

its X-CSRF-Token

sergei-u-niq
Active Contributor
0 Likes

your initial question was "how to use Bearer Token for authentication" => use "Authorization: Bearer xxxTokenxxx" for that.

raya_hemanth
Explorer
0 Likes

Hi ,

When requesting for token how to set header content length. I am getting error message "HTTP Error 411. The request must be chunked or have a content length." while requesting for token. Appreciate your help.

Thanks and regards,

~Hemanth