Home > Enterprise >  Error "302 Moved Temporarily" in "Test as add-on" mode (Telegram)
Error "302 Moved Temporarily" in "Test as add-on" mode (Telegram)

Time:10-31

  1. The spreadsheet contains project 1, deployed as a webapp with permissions: Execute as: Me, Who has access: Anyone.

Webapp

function doPost(e) {
  myLog('Received from Addon: '   JSON.stringify(e));
  // console.log('parameters from caller '   JSON.stringify(e));
  return ContentService.createTextOutput(JSON.stringify(e));
}

A webhook aTelegram-bot and this webapp is set.

  1. I am using this spreadsheet for testing (as add-on) of another project 2.

Add-on

function sendPost() {
  var sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();

  // var webAppUrl = "https://script.google.com/macros/s/#####/exec"; // 7: Part_1 - WebApp: My
  var webAppUrl = "https://script.google.com/macros/s/###/exec"; // 7: Part_1 - WebApp: Tester

  // var auth = ScriptApp.getOAuthToken();
  // var header = { 'Authorization': 'Bearer '   auth };
  var payload = { scriptName: 'updateData', sheetURL: 'sheetURL' };
  var options = {
    method: 'post',
    // headers: header,
    muteHttpExceptions: true,
    payload: payload
  };

  var resp = UrlFetchApp.fetch(webAppUrl, options);
  var respCode = resp.getResponseCode();
  console.log('resp: '   respCode);
  myLog(respCode);
  var respTxt = resp.getContentText();
  myLog('Response from webApp: '   respTxt);
  console.log('resp: '   respTxt);
}

Here is a short video of the process (EN-subtitles).

  1. I run sendPost() and everything works fine. Project 2 sends data to the webapp, which returns it. Since this is a Container-bound script and not a standalone one, I cannot watch the logs in the GCC logger. Therefore, I look at them in the custom logger and the entries are added normally.

Also https://api.telegram.org/bot{API_token}/getWebhookInfo shows that there are no errors:

{"ok":true,"result": {"url":"https://script.google.com/macros/s/###/exec", "has_custom_certificate":false, "pending_update_count":0, "max_connections":40,"ip_address":"142.250.***.***"}}
  1. Now I am sending a message from the chat with the bot. The doPost(e) function in the webapp accepts it and writes it to the spreadsheet. However, everything is not limited to one message. Requests from the bot come and go, and the logger creates more and more new rows in the spreadsheet. This happens until I redeploy the webapp with the doPost () function commented out. I tried to figure out if this is a limited loop or not. My patience was only enough for 20 such iterations, because as a result, the messages start repeating at intervals of about 1 minute. Then I have to reinstall the webhook.

In any case, it interferes with testing the addon.

  1. GetWebhookInfo is now showing that there is a "Wrong response from the webhook: 302 Moved Temporarily" error:

{"ok":true,"result": {"url":"https://script.google.com/macros/s/###/exec", "has_custom_certificate":false, "pending_update_count":1, "last_error_date":1635501472, "last_error_message":"Wrong response from the webhook: 302 Moved Temporarily", "max_connections":40,"ip_address":"142.250.1***.***"}}

  1. Googling revealed several possible reasons. From url to the script has changed to MITM in your network. I do not really believe in MITM and I suppose that this is due to the fact that the spreadsheet is open in testing mode as add-on and the URL of the webapp has changed in this mode. If so, then I'm not sure if this is the correct behavior of the testing system. In theory, such a situation should have been provided for and the webap url should remain unchanged. But maybe I'm wrong and the reason is different, so

QUESTION: Has anyone come across such a situation and will suggest a workaround on how to test a script as an addon in such conditions?

CodePudding user response:

refers to redirection. If ContentService is used, Google temporarily redirects the resource to a another domain to serve the content. This redirection is not performed when using HtmlService. So, if the issue is related to redirection, use HtmlService instead.

  • Related