Home > front end >  Request for JavaScript works, but not for GoogleScreen. What's the difference?
Request for JavaScript works, but not for GoogleScreen. What's the difference?

Time:05-15

Please, help. I can't find the error and I don't understand why the request is not working. It seems to me that it's just Google's IP blocked by the site I'm making a request to. The same request for JavaScript works.

function pleaseWork() {
    
      var myHeaders = {
        'contentType': 'application/json',
        'Authorization': 'Bearer 5cd4ac65-f71b-3eaa-93af-78610a7aa320',    
      }
      var raw = ["1111111111111"]
      
      var requestOptions = {
        'method': 'POST',
        'headers': myHeaders,
        'payload': JSON.stringify(raw) 
      };
      result = UrlFetchApp.fetch("https://www.ukrposhta.ua/status-tracking/0.0.1/statuses/last", requestOptions)
      Logger.log(result)
      
    }

Working Javascript request:

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer 5cd4ac65-f71b-3eaa-93af-78610a7aa320");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify([
  "1111111111111"
]);

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,  
};

fetch("https://www.ukrposhta.ua/status-tracking/0.0.1/statuses/last", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

How to do it right?

CodePudding user response:

In your Google Apps Script, the data is sent as form by default. When I saw your Javascript, it seems that the data is required to be sent as data with application/json. So you'll need to modify the script as follows.

From:

var requestOptions = {
  'method': 'POST',
  'headers': myHeaders,
  'payload': JSON.stringify(raw) 
};

To:

var requestOptions = {
  'method': 'POST',
  'headers': myHeaders,
  'payload': JSON.stringify(raw),
  'contentType': 'application/json',
};

Reference:

Note:

  • I think that this modification is the same as the request of your Javascript. But, if an error occurs, please confirm your values of raw and your access token, again.

Added 1:

Your showing script was modified for testing. Please set your access token and the value of raw and test it again.

function modified_pleaseWork() {
  var myHeaders = { 'Authorization': 'Bearer ###' };
  var raw = ["1111111111111"];
  var requestOptions = {
    'method': 'POST',
    'headers': myHeaders,
    'payload': JSON.stringify(raw),
    'contentType': 'application/json',
  };
  var result = UrlFetchApp.fetch("https://www.ukrposhta.ua/status-tracking/0.0.1/statuses/last", requestOptions);
  Logger.log(result.getContentText())
}

Added 2:

From your following reply,

but the solution doesn't work for me. I saw you added a modified script. You can also test it with the parameter "Bearer ###" Error 403. But it is not in the JavaScript code. I don't understand what's wrong?

I confirmed the status code 403 from your reply. In this case, it is considered that the site cannot be directly accessed from the Google side. Ref I think that the reason for your issue is due to this.

But, in the case of this situation, there is a workaround. Here, I would like to propose using this workaround. Ref

Sample script:

In this case, the request is run on the custom function. By this, I thought that the status code 403 might be able to be avoided. Actually, when I tested this script using your provided access token, the result value could be obtained.

In this script, the custom function is used. So, please copy and paste the following script to the container-bound script of Google Spreadsheet. And run main(). By this, the request is run on the custom function. And, the returned value can be retrieved with the script.

function modified_pleaseWork() {
  var myHeaders = { 'Authorization': 'Bearer 5cd4ac65-f71b-3eaa-93af-78610a7aa320' };
  var raw = ["1111111111111"];
  var requestOptions = {
    'method': 'POST',
    'headers': myHeaders,
    'payload': JSON.stringify(raw),
    'contentType': 'application/json',
  };
  var result = UrlFetchApp.fetch("https://www.ukrposhta.ua/status-tracking/0.0.1/statuses/last", requestOptions);
  return result.getContentText();
}

// Please run this function.
function main() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getRange("A1");
  range.setFormula("=modified_pleaseWork()");
  SpreadsheetApp.flush();
  const value = range.getValue();
  range.clearContent();
  console.log(value)
}
  • Related