Google Apps Script的内容服务返回HTML而不是JSON [英] Content Service for Google Apps Script returning HTML instead of JSON

查看:154
本文介绍了Google Apps Script的内容服务返回HTML而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Content Service API并且该示例在返回JSON时返回HTML,我在做什么错误?

https://developers.google.com/apps-script/guides/content

 函数doGet(request){
var events = CalendarApp.getEvents(
new Date(Number(request.parameters.start)* 1000),
new Date(Number(request.parameters.end)* 1000));
var result = {
available:events.length == 0
};
返回ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}

尝试发出请求的另一个文件中的GAS:

  function myFunction(){
var url =published URL;
url + =?start = 1325437200& end = 1325439000;

var options = {
method:get
}
var response = UrlFetchApp.fetch(url,options).getContentText();
response = JSON.parse(response); //错误,意外的令牌<


解决方案

您对ContentService的使用是正确的,代码完全按照原样工作。以下是我的代码副本作为网络应用程序发布的链接: b
$ b


您遇到的问题与授权或身份验证相关,如果发布的脚本未经授权,则返回HTML错误消息。



检查是否是您的问题,只需直接在浏览器中访问发布的网址。如果您看到JSON显示,则授权不是问题。如果您看到执行该操作需要授权错误消息,请打开您发布的脚本,然后从运行菜单中选择doGet,然后按照授权提示操作。



更有可能的是,这个问题与您的脚本如何发布有关。为了从其他脚本访问已发布的脚本,必须以谁有权访问应用设置为任何人,甚至是匿名发布该脚本。如果您使用任何其他值,则Google会返回一个HTML登录页面,而不是您的JSON响应,并且会看到您看到的错误。



这是因为Google发送的请求通过URLFetchApp的Apps脚本未通过身份验证,它们不携带用户运行代码的凭据,并以匿名请求的形式进入。

如果您在发布设置中不允许任何人,甚至匿名,Google会将未经过身份验证的请求重定向到Google登录页面。

Trying out the Content Service API and the example is returning HTML when it should be returning JSON, what am I doing wrong?

https://developers.google.com/apps-script/guides/content

function doGet(request) {
  var events = CalendarApp.getEvents(
    new Date(Number(request.parameters.start) * 1000),
    new Date(Number(request.parameters.end) * 1000));
  var result = {
    available: events.length == 0
  };
  return ContentService.createTextOutput(JSON.stringify(result))
    .setMimeType(ContentService.MimeType.JSON);
}

GAS from another file trying to make the request:

function myFunction() {
  var url = "published URL";
  url+="?start=1325437200&end=1325439000";

  var options = {
    method:"get"
  }
  var response = UrlFetchApp.fetch(url,options).getContentText();
  response = JSON.parse(response); //error, unexpected token <
}

解决方案

Your usage of ContentService is correct, the code works exactly as is. Here is a link to my copy of your code published as a web app:

https://script.google.com/macros/s/AKfycbzx2L643LHw0oQAq1jBmKQh2ju_znGfmdj78dUypj36iF-s91w/exec

The problem you are running into is related to Authorization or Authentication, if the published script is not authorized, an HTML error message is returned.

To check if that is your issue, simply access the published URL directly in your browser. If you see JSON displayed, then Authorization is not the problem. If you see the "Authorization is required to perform that action" error message, open your published script and choose "doGet" from the Run menu, then follow the authorization prompts.

More likely, the problem is related to how your script is published. In order to access your published script from another script, it must be published with the "Who has access to the app" setting as "Anyone, Even anonymous". If you use any other value, Google returns an HTML login page instead of your JSON response, and you get the error you are seeing.

This happens because requests sent from Google Apps Script via URLFetchApp are not authenticated, they don't carry the credentials of the user running the code with them, and come in as anonymous requests.

If you don't allow "Anyone, even anonymous" in your publishing settings, Google redirects non-authenticated requests to the Google login page.

这篇关于Google Apps Script的内容服务返回HTML而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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