如何使用Appscript doPost创建REST API? [英] How To Use Appscript doPost to create a REST API?

查看:75
本文介绍了如何使用Appscript doPost创建REST API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1个月后,我想再次提出有关该主题的问题: 这是包含所有链接的全文: https://docs.google. com/document/d/1Tb0-twzHl-wXbvaNF2IpCT0CiONT9PoqPlEweLz3oYI/edit

After 1 month, I would like to open the question regarding this topic once again: Here is the whole post with all links: https://docs.google.com/document/d/1Tb0-twzHl-wXbvaNF2IpCT0CiONT9PoqPlEweLz3oYI/edit

项目中有一个函数,其中urlFetchApp使用第二个脚本函数中的doPost发送有效负载. 第二个功能将有效负载存储在1.电子表格中:运行第一个功能时,第二个功能不将有效负载存储在电子表格中.详细说明如下:

There is a function in a project in which a urlFetchApp sends payload, using doPost in the second script function. The second function stores the payload in the 1. spreadsheet: When running the first function, the second function does not store the payload in the spreadsheet. Detailed description below:

1.具有脚本的项目

function merry2script() {
  var url = 'https://script.google.com/macros/s/AKfycbzM97wKyc0en6UrqXnVZuR9KLCf-UZAEpzfzZogbYApD9KChnnM/exec';
  var payload = {payloadToSend : 'string to send'};
  var method = 'post'
  var response = UrlFetchApp.fetch(url, {method : method, payload: payload}).getContentText();
  Logger.log(response);
  return;
}

img merry2.jpg

img merry2.jpg

2.具有脚本的项目

function doPost(e) {
  var ss = SpreadsheetApp.openById("0Apjz67q9b5PldFJUYkkzVGRHdGFYc1pFYWk5T0Eyc0E");
  var sheet = ss.getSheetByName("testsheet");
  var record;
  for (var i in e.parameters) {
    record = 'parameter: ' + i + ' = ' + e.parameters[i];
    sheet.getRange(sheet.getLastRow() + 1, 1, 1, 1).setValue(record);
  }
  var output = ContentService.createTextOutput();
  output.setContent("content to return");
  return output;
}

img merry_christmas.jpg

img merry_christmas.jpg

发布了2.脚本,

img发布证据2.脚本: mch1.jpg

img evidence of publishing 2. script : mch1.jpg

1.电子表格

1. spreadsheet https://docs.google.com/spreadsheet/ccc?key=0Apjz67q9b5PldFJUYkkzVGRHdGFYc1pFYWk5T0Eyc0E

结果: 通过hurl.it(网页)发送有效负载时,该有效负载不会显示在电子表格中. 尝试没有结果

Results: When the payload is sent through hurl.it(a webpage), it is not shown in the spreadsheet. trying without result

尝试使用此选项 安装此chrome扩展程序 高级REST客户端" 客户端/hgmloofddffdnphfgcellkdfbfbjeloo img结果

trying with this option install this chrome extension "Advanced REST Client" client/hgmloofddffdnphfgcellkdfbfbjeloo img result

电子表格中的结果 ss_test.jpg

result in spreadsheet ss_test.jpg

推荐答案

为了获得已发布URL的响应,必须使用以下设置对其进行发布:

In order to get response from the published URL it must be published with following settings:

  1. 以以下身份执行该应用程序:
  2. 有权访问该应用的人:甚至匿名的人
  1. Execute the app as: me
  2. Who has access to the app: Anybody even anonymous

这是必需的,因为在您获取URL的方式中,它只能访问公用的网址.

It is required because, the way you are fetching the URL, it can access only public available web address.

这篇关于如何使用Appscript doPost创建REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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