5

Problem

  • I want to communicate with api while changing IP address, because the server restricts IP address. So, I add X-Forwarded-For header with UrlFetchApp. However, I get an error "Attribute provided with invalid value: Header:X-Forwarded-For "

Question

  • How can I solve this problem? Or is there another way?
function myFetch() {

  var url = "https://XXXXXXXXX"

  var options = { method: "GET" };
  options.headers = {
    Authorization: "Bearer " + MY_TOKEN,
    "X-Forwarded-For": "XXX.XXX.XXX.XXX"
  };

  var resp = UrlFetchApp.fetch(url, options);
  var json = resp.getContentText();
  var data = JSON.parse(json);

  return data;
}

Tried → Error

  • Tried : cURL request. →Success : I can change IP address, and get data.
    curl -X GET \
      https://XXXXXXXXX \
      -H 'Authorization: Bearer MY_TOKEN' \
      -H 'X-Forwarded-For: XXX.XXX.XXX.XXX' \
  • Tried : Delete X-Forwarded-For header. →Error : "API key does not allow access from IP XXX.XXX.XXX.XXX"

  • Tried : I also add Host header. →Error : "Attribute provided with invalid value: Header:Host".

Does anybody have a solution for my problem? Best regards,

TheMaster
  • 45,448
  • 6
  • 62
  • 85
RyosukeOK
  • 686
  • 1
  • 9
  • 22
  • 1
    In your situation, how do you want to execute your script? Can you provide several executing methods that you can use? Because I thought that it is required to think of the solution and workaround for each executing method. – Tanaike Sep 14 '19 at 07:48
  • 3
    If you use a button to click and execute, X-Forwaded is automatically added. See [Tanaike's answer](https://stackoverflow.com/a/54622357) and [search](https://stackoverflow.com/search?q=%5Bgoogle-apps-script%5D+X-Forwarded-For) – TheMaster Sep 14 '19 at 08:50
  • Thanks for your comments. I want to execute with time-driven trigger. – RyosukeOK Sep 14 '19 at 10:00
  • 5
    Some HTTP Request Headers are internally managed by App Script and cannot be overridden from a `UrlFetchApp.fetch()` call. `X-Forwarded-For` appears to be one of those headers. You can try leveraging an intermediary service to handle that request such as a Google Cloud Function. – TheAddonDepot Sep 14 '19 at 13:13
  • Ok, I'll do this GCF. I decided thx. – RyosukeOK Sep 14 '19 at 13:23
  • 1
    I have created a feature request here : https://issuetracker.google.com/issues/267096387. Go +1 it – BorisD Jan 30 '23 at 12:49

0 Answers0