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

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

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

那麼已知一個Social Profile ID,如何用nodejs經過Web Service的方式得到該Profile明細?node

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

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

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

您能夠參考我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執行。app

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

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

相關文章
相關標籤/搜索