使用nodejs消费SAP Cloud for Customer上的Web service
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:///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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dnf90级后干什么
- 下一篇: 使用代码获得Netweaver里某个so