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,获取数据
posted @ 2022-02-16 19:44  追寻者2019  阅读(1287)  评论(0编辑  收藏  举报