1. 程式人生 > 其它 >ABAP http client REST/HTTP介面連線案例 SAAS平臺基於Oauth2認證的API

ABAP http client REST/HTTP介面連線案例 SAAS平臺基於Oauth2認證的API

Step1.示例程式碼

  1. 在APIPost SoapUI等軟體中,引數對應的ABAP方法
    TYPES: BEGIN OF ty_token,
              access_token TYPE char50,
              token_type   TYPE char10,
              expires_in   TYPE char10,
              scope        TYPE char10,
           END OF ty_token.
    
    DATA: go_http_client TYPE REF TO if_http_client.
    
    DATA: gt_token TYPE TABLE OF ty_token,
          gs_token TYPE ty_token.
    
    DATA: response TYPE string,
          message  TYPE string,
          token    TYPE char50.
    
    CONSTANTS: gc_host TYPE string VALUE 'http://api.host.com',
               gc_auth TYPE string VALUE 'Basic YTU50Tk1MzgtOTNmNC01NzYyLWJmNjMtZ5hjODQzNjIwYWEwOjdiZGY0NGVjLTc4NGUCNGU0NC0N2MyLWZiNTQ4OTY4MjdhMQ=='.
    
    
    *****如果有漢字需要轉碼成ADCII
    ****CALL METHOD cl_http_utility=>escape_url
    ****  EXPORTING
    ****    unescaped = string_in
    ****  RECEIVING
    ****    escaped   = string_out.
    
    ******* 建立客戶端請求
    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = gc_host
      IMPORTING
        client             = go_http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        others             = 4.
    
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH
                   sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        EXIT.
      ENDIF.
    
    ****go_http_client->propertytype_logon_popup = go_http_client->co_disabled.   "關閉登入彈窗
    
    ******* Step1:Get Token
    *http請求如果頻率高,Get Token應該另外寫程式,通過Background Job維持Token有效
    
    *設定http協議版本
    go_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
    *設定http請求方法
    go_http_client->request->set_method( if_http_request=>co_request_method_post ).
    
    *設定URI
    cl_http_utility=>set_request_uri( request = go_http_client->request uri = '/openapi/oauth.token' ).
    
    *設定http請求頭
    go_http_client->request->set_header_field( name = 'Accept' value = '*/*' ).
    go_http_client->request->set_header_field( name = 'accept-encoding' value = 'gzip, deflate, br' ).
    go_http_client->request->set_header_field( name = 'accept-language' value = 'zh-CN' ).
    go_http_client->request->set_header_field( name = 'connection' value = 'keep-alive' ).
    go_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
    go_http_client->request->set_header_field( name = 'charset' value = 'UTF-8' ).
    
    *Basic Auth:provide username:password in Base64 encoded in Authorization header
    go_http_client->request->set_header_field( name = 'Authorization' value = gc_auth ).
    
    *設定請求引數(請求引數也可以直接寫在URI)
    go_http_client->request->set_form_field( name = 'grant_type' value = 'client_credentials' ).
    go_http_client->request->set_form_field( name = 'scope' value = 'client' ).
    
    *傳送請求,並取得返回值
    PERFORM frm_process.
    response = go_http_client->response->get_cdata( ).
    
    *異常時取得錯誤訊息 IV_CP值如何獲得:SE37->NLS_GET_FRONTEND_CP
    ****message = cl_bcs_convert=>xstring_to_string( EXPORTING iv_xstr = go_http_client->response->get_raw_message( ) iv_cp = 8404 ).
    IF response IS INITIAL.
      MESSAGE 'Get Token Failed' TYPE 'E'.
    ELSE.
      PERFORM frm_set_token.                "JSON->結構/內表  儲存值給全域性TOKEN變數
    ENDIF.
    
    ******* Step2:Get data
    *清楚Token返回的資料
    CLEAR:response.
    
    *初始化http client物件
    PERFORM frm_refresh_obj.
    
    *設定http協議版本
    go_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
    *設定http請求方法
    go_http_client->request->set_method( if_http_request=>co_request_method_get ).
    
    *設定URI
    cl_http_utility=>set_request_uri( request = go_http_client->request uri = '/openapi/thirdparty/api/staff/v1/staffs/getStaffIdByMobileNo' ).
    
    *設定http請求頭
    go_http_client->request->set_header_field( name = 'Accept' value = '*/*' ).
    go_http_client->request->set_header_field( name = 'accept-encoding' value = 'gzip, deflate, br' ).
    go_http_client->request->set_header_field( name = 'accept-language' value = 'zh-CN' ).
    go_http_client->request->set_header_field( name = 'connection' value = 'keep-alive' ).
    go_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
    go_http_client->request->set_header_field( name = 'charset' value = 'UTF-8' ).
    
    *Auth By Token
    go_http_client->request->set_header_field( name = 'Authorization' value = |Bearer { token }| ).
    
    *設定請求引數(請求引數也可以直接寫在URI)
    go_http_client->request->set_form_field( name = 'mobileNo' value = '1818*******' ).
    
    *設定Body引數 SET_CDATA using JSON for string SET_DATA using JSON for bitycode
    ****go_http_client->request->set_cdata( data = var_string ).
    
    PERFORM frm_process.
    response = go_http_client->response->get_cdata( ).
    
    *異常時取得錯誤訊息 IV_CP值如何獲得:SE37->NLS_GET_FRONTEND_CP
    message = cl_bcs_convert=>xstring_to_string( EXPORTING iv_xstr = go_http_client->response->get_raw_message( ) iv_cp = 8404 ).
    
    IF response IS INITIAL.
      MESSAGE 'Get Data Failed' TYPE 'E'.
    ELSE.
      cl_demo_output=>display( response ).
    ENDIF.
    
    *處理結束,關閉連線
    CALL METHOD go_http_client->close.
    
    
    ******* FORM
    FORM frm_process.
      "傳送
      CALL METHOD go_http_client->send
        EXCEPTIONS
          http_communication_failure = 1
          http_invalid_state         = 2
          http_processing_failed     = 3
          http_invalid_timeout       = 4
          OTHERS                     = 5.
      "接收
      CALL METHOD go_http_client->receive
        EXCEPTIONS
          http_communication_failure = 1
          http_invalid_state         = 2
          http_processing_failed     = 3.
    ENDFORM.
    
    FORM frm_set_token.
    *JSON->內表
      /ui2/cl_json=>deserialize( EXPORTING json = response CHANGING data = gs_token ).
    
    *Filling token
      token = gs_token-access_token.
    ENDFORM.
    
    FORM frm_refresh_obj.
      CALL METHOD go_http_client->refresh_request
        EXCEPTIONS
          http_action_failed = 1
          OTHERS             = 2.
    
      IF sy-subrc <> 0.
    * Implement suitable error handling here
      ENDIF.
    
      CALL METHOD go_http_client->refresh_response
        EXCEPTIONS
          http_action_failed = 1
          OTHERS             = 2.
    
      IF sy-subrc <> 0.
    * Implement suitable error handling here
      ENDIF.
    
    ENDFORM.

    Step2.效果展示

    1. 獲得Toekn
    2. 拿Token是否成功,返回訊息
    3. 嵌入Token,獲取資料