• 如何处理消费SAP ABAP Web Service时遇到的Unauthorized错误


    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”.

    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.

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

    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”.

    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.

    How to resolve the error

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

    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:

    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”.

    Maintain user and password:

    (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.

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


    要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  • 相关阅读:
    [LeetCode#68] Text Justification
    [LeetCode#227] Basic Calculator II
    [LeetCode#87] Scramble String
    [LeetCode#280] Wiggle Sort
    [LeetCode#272] Closest Binary Search Tree Value II
    [LeetCode#254] Factor Combinations
    [LeetCode#174] Dungeon Game
    python报错集合
    在线画图工具
    xml模块(了解)
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/13614050.html
Copyright © 2020-2023  润新知