1. 程式人生 > >使用nodejs消費SAP Cloud for Customer上的Web service

使用nodejs消費SAP Cloud for Customer上的Web service

-o env acc 信用 *** dia point type LV

Jerry在公眾號文章C4C和微信集成系列教程裏曾經使用nodejs去消費C4C提供的標準webservice。

看一個具體例子:C4C裏Individual Customers可以維護Social User Profile,在Jerry上面的公眾號文章裏,正是把微信用戶的open ID維護到Social User Profile的SocialMediaAccountUserID字段去,如下圖所示。

技術分享圖片

那麽已知一個Social Profile ID,如何用nodejs通過Web Service的方式獲得該Profile明細?

首先到Administrator->Input and Output Management->Service Explorer中取得標準的查詢Social User profile的web service:

https://<host name>/sap/bc/srt/scs/sap/requestforsocialmediauserprofi

技術分享圖片

然後使用nodejs module request給這個url發一個HTTP post請求。

您可以參考我github上的源代碼。


var request = require(‘request‘);
var config = require("../../config.js");

function getSocialMediaProfile(profileID) {

  console.log("Jerry trace begin ***********************************");
  console.log("url: " + config.socialMediaProfileGetEndPoint);
  console.log("config.credential_qxl: " + config.credential_qxl);

  var ogetSocialMediaProfileOptions = {
        url: config.socialMediaProfileGetEndPoint,
        method: "POST",
        headers: {
            "content-type": "text/xml",
            ‘Authorization‘: ‘Basic ‘ + new Buffer(config.credential_qxl).toString(‘base64‘)
        },
        body: ‘<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:glob="http://sap.com/xi/SAPGlobal20/Global"><soapenv:Header/><soapenv:Body><glob:SocialMediaUserProfileRequest_sync>‘
                      +‘<SocialMediaUserProfileSelectionByElements>‘
                      +‘<SelectionBySocialMediaUserProfileID>‘
                      +‘<InclusionExclusionCode>I</InclusionExclusionCode>‘
                      +‘<IntervalBoundaryTypeCode>1</IntervalBoundaryTypeCode>‘
                      +‘<LowerBoundarySocialMediaUserProfileID >‘ + profileID + ‘</LowerBoundarySocialMediaUserProfileID>‘
                      +‘</SelectionBySocialMediaUserProfileID>‘
                      +‘</SocialMediaUserProfileSelectionByElements>‘
                      +‘</glob:SocialMediaUserProfileRequest_sync></soapenv:Body></soapenv:Envelope>‘
              };

  console.log("body: " + ogetSocialMediaProfileOptions.body);
  console.log("Jerry trace end ***********************************");

  return new Promise(function(resolve,reject){
      request(ogetSocialMediaProfileOptions,function(error,response,body){

        console.log("Jerry web service response: " + body);
        var soapreg = /.*<SocialMediaUserAccountID>(.*)<\/SocialMediaUserAccountID>.*/;
          var soapresult = soapreg.exec(body);
          if( soapresult.length === 2){
              resolve(soapresult[1]);
          }
      });
    }); 
}

module.exports = getSocialMediaProfile;

將上述代碼另存為文件getSocialMediaProfileTest.js, 直接使用node getSocialMediaProfileTest.js執行。

從console能觀察到發送的HTTP post請求的body和返回的響應內容:

技術分享圖片

技術分享圖片

要獲取更多Jerry的原創技術文章,請關註公眾號"汪子熙"或者掃描下面二維碼:

技術分享圖片

技術分享圖片

使用nodejs消費SAP Cloud for Customer上的Web service