从我的nodejs服务器调用谷歌应用程序脚本

时间:2022-08-20 20:29:30

I've developed a Google Apps Script and published it with no restrictions (everybody can see it), i get an url like this :

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

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

If i run this in a browser, it runs well.

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

Now I want to call this from my node js server. I use:

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

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

The httpResponse replies with a 302 and I've this in the headers:

httpResponse用302回复,我在标题中回答:

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

I believe that I need to do authentication before calling my request.post. But was unable to find anywhere in documentation how to do it.

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

1 个解决方案

#1


2  

Finally got a workaround (temporary) :

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

As it seems that the issue came from domains schemes difference (http on calling side, https Google Apps Script side), solution found was to implement the call client side in the web page, instead of server side, like this :

由于看起来问题来自域方案差异(呼叫端的http,https Google Apps脚本端),找到的解决方案是在网页中实现呼叫客户端,而不是服务器端,如下所示:

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 being called:

正在调用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);
}

This works, at the price of this not so nice JSONP technique, waiting i'm putting a certificate on my server, and switch to https://

这是有效的,以这个不太好的JSONP技术为代价,等待我将证书放在我的服务器上,然后切换到https://

#1


2  

Finally got a workaround (temporary) :

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

As it seems that the issue came from domains schemes difference (http on calling side, https Google Apps Script side), solution found was to implement the call client side in the web page, instead of server side, like this :

由于看起来问题来自域方案差异(呼叫端的http,https Google Apps脚本端),找到的解决方案是在网页中实现呼叫客户端,而不是服务器端,如下所示:

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 being called:

正在调用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);
}

This works, at the price of this not so nice JSONP technique, waiting i'm putting a certificate on my server, and switch to https://

这是有效的,以这个不太好的JSONP技术为代价,等待我将证书放在我的服务器上,然后切换到https://