如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤

i042416發表於2020-09-16

Previously I have written two documents about how to create document template with Word or Adobe PDF format which support web service created by Web service tool available in CRM Web client UI:

(1)  Create Webservice enabled word document in attachment assignment block (2)  Create Webservice enabled Adobe PDF attachment in CRM Application

Besides that you could still use external web service from third party or created from a remote function module via tcode SOAMANAGER in ABAP system.

However when you paste the URL of your WSDL as below and press enter key, you meet with the error message “Unauthorized”.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


Before we start to trouble shooting, we’d better quickly go through this document  how to create and consume web service in ABAP system to have a basic idea about web service creation and consumption in ABAP system.

Why we will see this Unauthorized error message

The position of this error message could easily be found via the tip described in my blog  How to quickly locate the source code where raises a given message in webclient UI:

The WSDL URL we input is used to initialize one instance of cl_http_client and then cl_http_client~send and cl_http_client~receive are called to get the content of this WSDL.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


Then we get expected 401 Unauthorized error, since we didn’t specify any credentials in this generic code.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


go to tcode SICF and find the ICF node /default_host/sap/bc/ ( which is the parent node of the ICF service generated for our web service), the logon procedure for it is set as “Standard”.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


According to  SAP help, Standard logon procedure means the framework will do authentication check based on the following orders until one avalid credential found. In our case, none of the seven attempts succeed, so finally we see 401 Unauthorized error.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


How to resolve the error

According to SAP help, after I add the following code to enable HTTP Basic Authentication, the error is gone.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


Of course this could not be used in productive code, we need to find a more generic solution. Go outer callstack of 401 error, I find there is another way to initialize http client instance by RFC destination instead ( line 49 ). Doing a where used list on class attribute mv_destination and soon I found there is a BAdI definition which could allow us to specify a HTTP destination for the external web service being used:


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


So the solution would be:

(1) Create a HTTP destination in tcode SM59 Maintain “Target Host”, “Service No.”( that is port number ) and paste the left part of WSDL URL to “Path Prefix”.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


Maintain user and password:


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


(2) Create a BAdI implementation on BAdI definition CRM_OI_WEBSERVICE_BADI

Implement the method GET_SERVICE_DESTINATION. fill the changing parameter cv_destination. You could do filtering based on the importing WSDL URL and Operation, in this example I just hard code the name of RFC destination created in step one.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


Now this destination will be used to create http client instance and the 401 error message is then resolved.


如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤 如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

如何處理消費SAP ABAP Web Service時遇到的Unauthorized錯誤


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2721607/,如需轉載,請註明出處,否則將追究法律責任。

相關文章