为了回传凭证状态到第三方系统,需要通过HTTPS的方式调用第三方API
1、获取第三方API token
DATA:
lv_url TYPE string, "http 服务接口地址.
lv_auth TYPE string,
lv_http_client TYPE REF TO if_http_client,
lv_msg TYPE string,
lv_postmesage TYPE string,
lv_len TYPE i,
lv_subrc TYPE sy-subrc,
- lv_timeout TYPE
json_ser TYPE REF TO cl_trex_json_serializer,
lv_json_str TYPE string,
lv_respon TYPE string,
lv_postdata TYPE string,
ls_zfit059 TYPE zfit059,
lt_form TYPE tihttpnvp,
ls_form LIKE LINE OF lt_form,
ls_token_error TYPE typ_token_error,
serializer TYPE REF TO zcl_trex_json_serializer,
deserializer TYPE REF TO zcl_trex_json_deserializer.
"创建实例
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lv_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
"获取token-设置POST服务
CALL METHOD lv_http_client->request->set_method( if_http_request=>co_request_method_post ).
"获取token-设置传输内容
CALL METHOD lv_http_client->request->set_header_field( name = ‘Authorization’ value = lv_auth ).
CALL METHOD lv_http_client->request->set_content_type( content_type = ‘form-data’).
ls_form-name = ‘grant_type’.
ls_form-value = ‘client_credentials’.
APPEND ls_form TO lt_form.
ls_form-name = ‘scope’.
ls_form-value = ‘write’.
APPEND ls_form TO lt_form.
CALL METHOD lv_http_client->request->set_form_fields( fields = lt_form ).
"发送请求
CALL METHOD lv_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 lv_http_client->get_last_error
IMPORTING
code = lv_subrc
message = lv_msg.
MESSAGE lv_msg TYPE c_s DISPLAY LIKE c_e.
LEAVE LIST-PROCESSING.
ELSE.
CALL METHOD lv_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
lv_respon = lv_http_client->response->get_cdata( ).
FIND 'error' IN lv_respon.
IF sy-subrc EQ 0.
"有错误
CREATE OBJECT deserializer.
CALL METHOD deserializer->deserialize(
EXPORTING
json = lv_respon
IMPORTING
abap = ls_token_error ).
MESSAGE ls_token_error-message TYPE c_s DISPLAY LIKE c_e.
LEAVE LIST-PROCESSING.
ELSE.
"获取token
CREATE OBJECT deserializer.
CALL METHOD deserializer->deserialize(
EXPORTING
json = lv_respon
IMPORTING
abap = ls_token_ok ).
TRY.
CATCH cx_trex_serialization.
ENDTRY.
ENDIF.
ENDIF.
2、URL中添加token 访问第三方API
"与token拼接
lv_url_er = lv_url && ‘/api/open/expenseReport/v2/paid?access_token=’.
CONCATENATE lv_url_er ls_token_ok-access_token INTO lv_url_er.
"创建实例
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url_er
IMPORTING
client = lv_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
"设置POST服务
CONCATENATE 'Bearer' ls_token_ok-access_token INTO lv_auth SEPARATED BY space.
CALL METHOD lv_http_client->request->set_header_field( name = 'Authorization' value = lv_auth ).
"设置传输内容
CLEAR ls_bxd.
ls_bxd-businesscode = gt_alv_send-businessno.
ls_bxd-documentcompanycode = 'Chlitina_SH'.
ls_bxd-employeeid = 'XN01'."虚拟员工号,固定
ls_bxd-comment = gt_alv_send-vblnr.
"序列化
CREATE OBJECT serializer
EXPORTING
data = ls_bxd.
CALL METHOD serializer->serialize( ).
CALL METHOD serializer->get_data
RECEIVING
rval = lv_json_send.
REPLACE ALL OCCURRENCES OF 'businesscode' IN lv_json_send WITH 'businessCode'.
REPLACE ALL OCCURRENCES OF 'documentcompanycode'IN lv_json_send WITH 'documentCompanyCode'.
REPLACE ALL OCCURRENCES OF 'employeeid' IN lv_json_send WITH 'employeeID'.
lv_len = strlen( lv_json_send ).
“Json格式
CALL METHOD lv_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8').
CALL METHOD lv_http_client->request->set_method( if_http_request=>co_request_method_post ).
CALL METHOD lv_http_client->request->set_cdata(
EXPORTING
data = lv_json_send
offset = 0
length = lv_len ).
"发送请求
CALL METHOD lv_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
CALL METHOD lv_http_client->get_last_error
IMPORTING
code = lv_subrc
message = lv_msg.
IF lv_subrc <> 0.
ELSE.
CALL METHOD lv_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
CLEAR:lv_subrc,lv_msg.
CALL METHOD lv_http_client->get_last_error
IMPORTING
code = lv_subrc
message = lv_msg.
IF lv_subrc <> 0.
ELSE.
”获取返回结果
lv_json = lv_http_client->response->get_cdata( ).
REPLACE ALL OCCURRENCES OF 'errorCode' IN lv_json WITH 'errorcode'.
CREATE OBJECT deserializer.
"将返回结果解析到工作区
CALL METHOD deserializer->deserialize(
EXPORTING
json = lv_json
IMPORTING
abap = ls_res ).
IF ls_res-errorcode IS INITIAL.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.