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

i042416發表於2018-06-17

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

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

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

那麼已知一個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消費SAP Cloud for Customer上的Web service

然後使用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和返回的響應內容:

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

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

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

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

相關文章