从我的nodejs服务器调用google apps脚本

我开发了一个Google Apps脚本,并且没有任何限制地发布它(每个人都可以看到它),我得到了这样一个url:

https://script.google.com/macros/s/<scriptid>/exec 

如果我在浏览器中运行它,它运行良好。

现在我想从我的节点js服务器调用这个。 我用:

 request.post({url:url,form:{<my parameters>},function (err,httpResponse,body) 

httpResponse回复一个302,我已经在头文件中:

 "CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 

我相信我需要在调用我的request.post之前进行身份validation。 但无法find文件中的任何地方如何做到这一点。

最后得到了一个解决办法(临时):

由于问题似乎来自域名scheme差异(http呼叫方,https Google Apps脚本方),因此find的解决scheme是在网页中实现呼叫客户端,而不是服务器端,如下所示:

 var googleAppsScript = "https://script.google.com/macros/s/<myScriptId>/exec"; function scriptCB(result) { if (result=="KO") displayError("Something went wrong in apps script"); else displayMessage(result); } function callGAppsScript(docId,fields,cb) { $.ajax({ crossDomain: true, //REQUIRED !! as no scheme matched between my site and script.google.com type:'POST', url: googleAppsScript, contentType: "application/json; charset=utf-8", data: {prefix:'scriptCB',docId:docId,fields:JSON.stringify(fields)}, dataType: "jsonp", //REQUIRED as we do Cross domain jsonp:false, //disable adding ?callback= parameter jsonpCallback:'scriptCB', //my callback once it is done error: function(xhr,status,err) { alert(err); } }); } 

Google Apps脚本被调用:

 //script to replace some tagged fields in a given doc function doGet(e) { var docId=e.parameter.docId; var fields=JSON.parse(e.parameter.fields); var doc = openDoc(docId); //my function to open the Google Document var result="Document not found : "+docId; if (doc) { result="Doc "+doc.getName()+" open. Fields to replace: "+JSON.stringify(fields); var _result = replaceFields(doc,fields); //my function to replace fields with new values brought by the external REST call if (_result!="KO") result=_result; } return ContentService.createTextOutput( e.parameters.prefix + '(' + JSON.stringify(result) + ')') //this is necessary to get the jsonP callback working. .setMimeType(ContentService.MimeType.JAVASCRIPT); //that's too. } function doPost(e) { return doGet(e); } 

这工作,在这不是很好的JSONP技术的价格,等待我把我的服务器上的证书,并切换到https://