0

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?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • 'Authorization': 'Bearer 5cd4ac65-f71b-3eaa-93af-78610a7aa320' These parameters should not be changed. – Олег Барсуков May 14 '22 at 19:29
  • Error. Exception: Request failed for https://www.ukrposhta.ua returned code 403. Truncated server response: 403 Forbidden – Олег Барсуков May 15 '22 at 05:53

1 Answers1

1

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)
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • That's weird, providing a function named `fetch` that doesn't implement the standard web fetch API. – Bergi May 15 '22 at 01:29
  • "But, if an error occurs, please confirm your values of raw and your access token, again." - how to do it? – Олег Барсуков May 15 '22 at 06:38
  • @Олег Барсуков Thank you for replying. About `"But, if an error occurs, please confirm your values of raw and your access token, again." - how to do it?`, by running the script with setting your value of `raw` and your access token, you can confirm whether an error occurs. – Tanaike May 15 '22 at 06:45
  • @Tanaike. When creating a theme, I submitted a similar request for JavaScript and it works. Please understand that all information is correct. You can check it in two clicks. Press F12 in the browser and simply paste the JavaScript request into the console, press Enter. See successful completion. But in Google Aps the query doesn't work for some reason. All suggested options did not work in my case..( – Олег Барсуков May 15 '22 at 08:00
  • Please tell me. Perhaps the request in the form you suggested works for you? I also want to note that in the documentation for the API, I must specify the content type in "Header". If you check the JavaScript request, it is in "Header" that the content type is specified and it works. – Олег Барсуков May 15 '22 at 08:03
  • @Олег Барсуков Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand your 1st reply. About your 2nd reply, about `Please tell me. Perhaps the request in the form you suggested works for you?`, unfortunately, I have no account of the API you want to use. So, I modified your Google Apps Script with the provided Javascript you confirmed. The request is the same between your Javascript and my proposed script. – Tanaike May 15 '22 at 08:17
  • @Олег Барсуков By the way, I added a whole modified script in my answer. Could you please confirm it? When you test this script, please set your access token and the value of `raw ` and test it again. I deeply apologize that I have no account of the API you want to use, again. – Tanaike May 15 '22 at 08:22
  • @Олег Барсуков As additional information, please be careful that the properties of `'contentType'` and `"Content-Type"` are different. When you use `"Content-Type"`, please include it in the request header. In Google Apps Script, when you use `'contentType'`, please don't include it in the request header. – Tanaike May 15 '22 at 08:31
  • @Tanaike. Thank you very much for your attention, but the solution doesn't work for me. I saw you added a modified script. You can also test it with the parameter "Bearer 5cd4ac65-f71b-3eaa-93af-78610a7aa320" Error 403. But it works in Javascript. I don't understand what's wrong? – Олег Барсуков May 15 '22 at 08:48
  • @Олег Барсуков Thank you for replying. From your reply and your provided access token, I could correctly understand your current issue. By this, I added a sample script of a workaround for avoiding this issue. Could you please confirm it? If that was not useful, I apologize. – Tanaike May 15 '22 at 09:00
  • @Tanaike. It's something unreal. How? Why? Why does it work? You have done something fantastic and incomprehensible. If a function is pasted into a sheet and then its values ​​are copied over, then it works. Why, please explain. It's just a head break. – Олег Барсуков May 15 '22 at 09:13
  • @Олег Барсуков Thank you for replying. I'm glad your issue was resolved. About your additional question, I remembered my answer. https://stackoverflow.com/a/63024816 It seems that the request for a custom function is different from the request directly requested with the script editor. I thought that this situation might be able to become a workaround. In your situation, this workaround could be used. But, I think that this workaround cannot be all situations. So, when you use another URL, please be careful about this. – Tanaike May 15 '22 at 09:18