我开发了一个Google Apps脚本并且没有任何限制地发布它(每个人都可以看到它),我得到这样的网址:
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
之前我需要进行身份验证。但无法在文档中的任何地方找到如何做到这一点。
答案 0 :(得分:2)
最后得到了一个解决方法(临时):
由于看起来问题来自域方案差异(呼叫端的http,https Google Apps脚本端),找到的解决方案是在网页中实现呼叫客户端,而不是服务器端,如下所示:< / p>
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://