如何在谷歌应用程序脚本上调用Web服务

时间:2018-05-23 14:54:29

标签: web-services soap google-apps-script binding sap

我在SAP中创建了一个Web服务,并尝试从谷歌应用程序脚本调用此SOAP Web服务。

这个WS处于BASIC认证中,在变量响应中,我收到了与传入输入相同的wsdl。 我在后端系统调用中遇到问题“收到不支持的内容类型:”“ 有人可以帮帮我吗?

var user = 'user';
var pwd = 'pwd';
var webservice = 'http://xx.xxx.xx.xx:xxxx/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zdemo_google_ws/100/zdemo_google_ws/zdemo_google_ws?sap-client=100.wsdl';
function readData() {  

//Clean up anything currently on the spreadsheet  
SpreadsheetApp.getActiveSheet().clear();  

try {  

//Various XML parsing.  
var xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">"
+"<soapenv:Header/>"
+"   <soapenv:Body>"
+"     <urn:ZDemoGoogle>"
+"         <ItWebs>"
+"            <item>"
+"               <Protocol></Protocol>"
+"               <Tasktype></Tasktype>"
+"               <EntryId></EntryId>"
+"               <Account></Account>"
+"               <Mandt></Mandt>"
+"               <Host></Host>"
+"               <Port></Port>"
+"               <Destination></Destination>"
+"               <Path></Path>"
+"               <Counter></Counter>"
+"               <Calltime></Calltime>"
+"               <ExecutionTi></ExecutionTi>"
+"               <DataSend></DataSend>"
+"               <DataSendTi></DataSendTi>"
+"               <DataReceive></DataReceive>"
+"               <DataReceiveTi></DataReceiveTi>"
+"            </item>"
+"         </ItWebs>"
+"      </urn:ZDemoGoogle>"
+"   </soapenv:Body>"
+"</soapenv:Envelope>"

var options = {  
  headers: {  
    "Authorization": "Basic " + Utilities.base64Encode(user + ':' + pwd),
    "method" : "post",
    "contentType" : "text/xml",
    "payload" : xml,
    "muteHttpExceptions":true
  },  
};  

//UrlFetchApp is a powerful built-in library from Google  

var serviceaddress =  webservice ;
var response = UrlFetchApp.fetch(serviceaddress, options);

1 个答案:

答案 0 :(得分:0)

要使用UrlFetchApp.fetch(),需要修改脚本的option。那么这个修改怎么样?

来自:

var options = {  
  headers: {  
    "Authorization": "Basic " + Utilities.base64Encode(user + ':' + pwd),
    "method" : "post",
    "contentType" : "text/xml",
    "payload" : xml,
    "muteHttpExceptions":true
  },  
};

致:

var options = {  
  headers: {"Authorization": "Basic " + Utilities.base64Encode(user + ':' + pwd)},
  method: "post",
  contentType: "text/xml",
  payload: xml,
  muteHttpExceptions: true,
};

参考:

如果这不是您问题的直接解决方案,我很抱歉。