如何将Google Apps Script ContentService用作REST服务器 [英] How to use Google Apps Script ContentService as a REST server

查看:122
本文介绍了如何将Google Apps Script ContentService用作REST服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Google Apps脚本中创建jSon响应的服务

$ p $函数doGet(e){
返回ContentService
.createTextOutput(mcpher.getRestLibrary()。serialize())
.setMimeType(ContentService.MimeType.JSON);
}

这可以在 mcpher.com



当我在浏览器上运行它,或者使用firefox RESTclient,它会给出正确的响应。然而,当我在VBA中使用httpGET时,或者在Google Apps脚本中使用httpGET时

  function restTest(){
var x = UrlFetchApp.fetch(http://script.google.com/a/macros/mcpher.com/s/AKfycbzLqpnQ2ey8CKAMmzchb2n2FU-aiae0iTKPzAOfAgEpxGwaJgk/exec);
mcpher.DebugPrint(x);
}

我得到了这个回应..就像登录请求一样

  {0:\\\
< html dir = \ltr \> \\\
< head> \\\
< meta name = \robots \content = \\noindex,nofollow \/> \\\
< title> mcpher.com< / title> \\\
< script type = \text / javascript\> \\\
< ;! - \\\
//保持登录页面不被iframes所困。\\\ n ... etc ...

该脚本设置为任何人都可以访问,我是否需要进行身份验证 - 即使我是从同一个浏览器会话运行它的?

解决方案

它需要设置为任何人都可以访问,包括匿名。你确定这是设置?


I have a service that creates a jSon response in Google Apps Script

function doGet(e) {
   return ContentService
            .createTextOutput(mcpher.getRestLibrary().serialize())
            .setMimeType(ContentService.MimeType.JSON);
}

This can be found at mcpher.com

When i run it at the browser, or the firefox RESTclient it gives a correct response. However when I use httpGET from within VBA, or as in the example below in Google Apps Script

function restTest() {
  var x = UrlFetchApp.fetch("http://script.google.com/a/macros/mcpher.com/s/AKfycbzLqpnQ2ey8CKAMmzchb2n2FU-aiae0iTKPzAOfAgEpxGwaJgk/exec");
  mcpher.DebugPrint(x);
}

I get this response .. like a request to login

{"0":"\n<html dir=\"ltr\" >\n<head>\n  <meta name=\"robots\" content=\"noindex,nofollow\" />\n  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n  <title>mcpher.com</title>\n  <script type=\"text/javascript\">\n  <!--\n    // Keep login page from being trapped in iframes.\n  ... etc...

The script is set to anybody can access. Do I somehow have to authenticate - even though I'm running it from the same browser session ?

解决方案

It needs to be set to anyone can access, including anonymous. Are you sure that's the setting?

这篇关于如何将Google Apps Script ContentService用作REST服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆