集成场景:SAP ECC与第三方系统集成交互,中间件使用的是SAP PO,SAP ECC作为主调方,主动调用第三方系统,访问第三方系统需要Token验证
调用第三方分为以下几步:
- 获取第三方系统的Token
- 根据获取到的第三方系统的Token再进行调用第三方系统接口
处理步骤:
- SAP PO集成配置
- SAP ECC配置与代码开发
具体步骤:
第一步:获取第三方系统的Token
SAP PO端配置:
ESR配置:
- 定义FUNCTION LIBRARIES
- 定义DATA TYPE
- 定义MESSAGE TYPE
- 定义SERVICE INTERFACE
定义FUNCTION LIBRARIES
public String set_token(String var1, String var2, Container container) throws StreamTransformationException{
DynamicConfiguration dynamicConfig = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/REST","token");
dynamicConfig.put(key,var1); //token is the parameter containing the value
return var2;
}
定义DATA TYPE
定义MESSAGE TYPE
定义SERVICE INTERFACE
outbound服务
Inbound服务
IB配置
Communication Channel通信渠道配置:
调用方通信渠道定义:
被调方通信渠道定义:
维护REST RUL:
调用第三方系统提供的获取Token的接口地址:
维护REST Operation:
维护data format:
维护Http Header
Integrated Configuration配置:
SAP ECC端配置和代码开发
事务码:SPROXY
生成服务代理类
创建一个function module,去获取SRM token
FUNCTION zjksrm243.
*"----------------------------------------------------------------------
*"*"本地接口:
*" EXPORTING
*" REFERENCE(CODE) TYPE STRING
*" REFERENCE(ACCESS_TOKEN) TYPE STRING
*" REFERENCE(TOKEN_TYPE) TYPE STRING
*" REFERENCE(EXPIRES_IN) TYPE STRING
*" REFERENCE(MSG) TYPE STRING
*" REFERENCE(SUCCESS) TYPE STRING
*"----------------------------------------------------------------------
DATA: ls_srmtoken TYPE ztsrm_token,
lo_srm TYPE REF TO zco_si_sap_srm003_req,
ls_output TYPE zmt_srm003_req,
ls_input TYPE zmt_srm003_resp,
lv_msgguid TYPE sxmsmguid,
ls_log TYPE ztpi_if_log01,
lc_cx_fault TYPE REF TO cx_ai_system_fault,
lc_ap_fault TYPE REF TO cx_ai_application_fault.
CLEAR: ls_srmtoken,
ls_output,
ls_input.
* 获取SRM授权信息
SELECT SINGLE * INTO ls_srmtoken
FROM ztsrm_token
WHERE sysid = sy-sysid.
IF sy-subrc <> 0.
success = 'false'.
msg = '请配置SRM授权信息'.
RETURN.
ENDIF.
"* 接口日志
"inbound / outbound
ls_log-ziftype = 'O'.
ls_log-zifno = 'SI_SRM003'.
ls_log-zifdes = '获取SRM token'.
CALL FUNCTION 'ZPI_001_F_IF_LOG'
EXPORTING
im_proc = 'S'
CHANGING
cs_log = ls_log.
TRY.
CREATE OBJECT lo_srm.
CATCH cx_ai_system_fault.
ENDTRY.
ls_output-mt_srm003_req-tenant_id = ls_srmtoken-tenant_id. "tenantId
ls_output-mt_srm003_req-username = ls_srmtoken-username. "用户名
ls_output-mt_srm003_req-password = ls_srmtoken-password. "密码
ls_output-mt_srm003_req-grant_type = ls_srmtoken-grant_type. "授权类型
ls_output-mt_srm003_req-scope = ls_srmtoken-scope. "范围
ls_output-mt_srm003_req-type = ls_srmtoken-type. "类型
TRY .
CALL METHOD lo_srm->si_sap_srm003_req
EXPORTING
output = ls_output
IMPORTING
input = ls_input.
CATCH cx_ai_system_fault INTO lc_cx_fault.
MSG = lc_cx_fault->get_text( ). " Exception handling logic
ENDTRY.
"* 取得Msg Guid (Outbound)
DATA: lr_proto_msg_id TYPE REF TO if_wsprotocol_message_id.
TRY.
lr_proto_msg_id ?=
lo_srm->get_protocol( if_wsprotocol=>message_id ).
lv_msgguid = lr_proto_msg_id->get_message_id( ).
CATCH cx_ai_system_fault INTO lc_cx_fault.
MSG = lc_cx_fault->get_text( ). " Exception handling logic
ENDTRY.
ls_log-msgguid = lv_msgguid.
ls_log-zprocstatus = ls_input-mt_srm003_resp-code.
ls_log-zprocmessage = ls_input-mt_srm003_resp-msg.
ls_log-zprocdocext = ls_input-mt_srm003_resp-access_token.
access_token = ls_input-mt_srm003_resp-access_token.
success = ls_input-mt_srm003_resp-success.
token_type = ls_input-mt_srm003_resp-token_type.
code = ls_input-mt_srm003_resp-code.
expires_in = ls_input-mt_srm003_resp-expires_in.
CALL FUNCTION 'ZPI_001_F_IF_LOG'
EXPORTING
im_proc = 'E'
CHANGING
cs_log = ls_log.
" * 清理内存
CLEAR: lo_srm,
lc_cx_fault,
lc_ap_fault.
ENDFUNCTION.
第二步:根据获取到的Token再进行调用第三方系统
SAP PO配置:
ESR配置
- 定义DATA TYPE
- 定义MESSAGE TYPE
- 定义SERVICE INTERFACE
- 定义MESSAGE MAPPING
- 定义OPERATION MAPPING
定义DATA TYPE
调用方
被调方
定义MESSAGE TYPE
调用方
被调方:
定义SERVICE INTERFACE
被调方
定义MESSAGE MAPPING
Request Message Type Mapping
Response Message Type Mapping
注意:在做message mapping的时候,需要将Request 和Response的Message Type的每一个节点做mapping匹配处理
定义OPERATION MAPPING
IB配置:
通信渠道配置:
调用方
被调方
维护REST URL
Integrated Configuration
SAP ECC端配置和代码开发
事务码:SPROXY
生成代理类
DATA: ls_srm001 TYPE REF TO zco_si_sap_srm001_req,
lc_cx_fault TYPE REF TO cx_ai_system_fault,
lc_ap_fault TYPE REF TO cx_ai_application_fault,
lr_proto_msg_id TYPE REF TO if_wsprotocol_message_id,
ls_output TYPE zmt_srm001_req,
ls_input TYPE zmt_srm001_resp,
lv_msg TYPE string,
ls_srm001 TYPE REF TO zco_si_sap_srm001_req.
DO 10 TIMES."获取TOKEN,调用第一步封装好的函数ZJKSRM243来获取Token
CLEAR: lv_msg,
lv_code,
lv_token,
lv_token_type,
lv_expires_in,
lv_success.
CALL FUNCTION 'ZJKSRM243'
IMPORTING
code = lv_code
access_token = lv_token
token_type = lv_token_type
expires_in = lv_expires_in
msg = lv_msg
success = lv_success.
IF lv_token IS NOT INITIAL.
EXIT.
ELSE.
WAIT UP TO 1 SECONDS.
ENDIF.
ENDDO.
IF lv_msg IS NOT INITIAL.
MESSAGE lv_msg TYPE 'E'.
ENDIF.
*ls_output赋值处理
*XXXXXXXXXX
*XXXXXXXXX
*XXXXXXXXXX
* 接口日志
"inbound / outbound
ls_log-ziftype = 'O'.
ls_log-zifno = 'SI_SRM001'.
ls_log-zifdes = 'SRM采购订单同步接口'.
CALL FUNCTION 'ZPI_001_F_IF_LOG'
EXPORTING
im_proc = 'S'
CHANGING
cs_log = ls_log.
TRY.
CREATE OBJECT ls_srm001.
CATCH cx_ai_system_fault.
lv_msg = lc_cx_fault->get_text( ).
ENDTRY.
TRY .
CLEAR ls_input.
CALL METHOD ls_srm001->si_sap_srm001_req
EXPORTING
output = ls_output
IMPORTING
input = ls_input.
CATCH cx_ai_system_fault INTO lc_cx_fault.
* MESSAGE lc_cx_fault->get_text( ) TYPE 'E'. " Exception handling logic
lv_msg = lc_cx_fault->get_text( ).
ENDTRY.
"* 取得Msg Guid (Outbound)
TRY.
lr_proto_msg_id ?=
ls_srm001->get_protocol( if_wsprotocol=>message_id ).
lv_msgguid = lr_proto_msg_id->get_message_id( ).
CATCH cx_ai_system_fault INTO lc_cx_fault.
MESSAGE lc_cx_fault->get_text( ) TYPE 'E'. " Exception handling logic
ENDTRY.
IF ls_input-mt_srm001_resp-res_code = '200000'.
MODIFY ztajkyj004_log FROM TABLE lt_save_log.
ENDIF.
ls_log-msgguid = lv_msgguid.
ls_log-zprocdocerp = gs_ekko-ebeln.
ls_log-zprocstatus = ls_input-mt_srm001_resp-res_code.
ls_log-zprocmessage = ls_input-mt_srm001_resp-res_msg && ls_input-mt_srm001_resp-data-error.
CALL FUNCTION 'ZPI_001_F_IF_LOG'
EXPORTING
im_proc = 'E'
CHANGING
cs_log = ls_log.
"清理内存
CLEAR: ls_srm001,
lc_cx_fault,
lc_ap_fault,
lr_proto_msg_id.