How To Use Appscript doPost to create a REST API?

天涯浪子 提交于 2020-08-25 07:56:22

问题


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

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. project with script

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

2. project with script

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

published 2. script ,

img evidence of publishing 2. script : mch1.jpg

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

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

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

result in spreadsheet ss_test.jpg


回答1:


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

  1. Execute the app as: me
  2. Who has access to the app: Anybody even anonymous

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




回答2:


You just need to include the headers key into your dictionary that's all And of course on top of what Hari Das mentioned

headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}

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


来源:https://stackoverflow.com/questions/14034140/how-to-use-appscript-dopost-to-create-a-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!