在Google Sheets中调用客户端IP的URL是否可能?

huangapple go评论67阅读模式
英文:

Is it possible in Google Sheets to call a URL with the client's IP

问题

I need users to receive a response from a certain server based on the token they type, but if many incorrect responses are sent to it, the server blocks the IP address that sent the wrong token.

Since I don't want the Google Script server to be blocked, is there a possibility to make the client send the request through his private IP?

Right now what I'm doing is a decidedly inelegant form. I ask the client to enter the link and enter the response to SpreadsheetApp.getUi().prompt();

英文:

I need users to receive a response from a certain server based on the token they type, but if many incorrect responses are sent to it, the server blocks the IP address that sent the wrong token
Since I don't want the Google Script server to be blocked, is there a possibility to make the client send the request through his private IP?

Right now what I'm doing is a decidedly inelegant form
I ask the client to enter the link and enter the response to SpreadsheetApp.getUi().prompt();

答案1

得分: 1

根据描述,我会假设为了从服务器获得响应,正在使用URL提取服务。如果是这样的话,API文档指出请求始终来自特定的IP范围,这意味着从Apps Script后端是不可能的,但有一种解决方法,但需要

  1. 创建一个侧边栏
  2. 运行一个fetch()函数(这在AJAX中运行,因此在客户端运行)

结果会看起来像这样:

在Google Sheets中调用客户端IP的URL是否可能?

HTML文件在此示例中称为index,看起来像这样:

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
      </head>
      <body>
        <input type="button" onclick="fetching()"value="clickme"></input>
        </br>
        <div id="field"></div>
        <script>
        function fetching(){
        fetch('https://httpbin.org/anything')
        .then(response => response.json()) 
        //.then(json => console.log(json))
        .then(json => document.getElementById("field").innerHTML= "Response: "+JSON.stringify(json.origin))
         }
      </script>
      </body>
    </html>

代码文件看起来像这样:

    function onOpen() {
      SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
          .createMenu('Custom Menu')
          .addItem('Show sidebar', 'showSidebar')
          .addToUi();
    }
    
    function showSidebar() {
      var html = HtmlService.createHtmlOutputFromFile('index.html')
          .setTitle('My custom sidebar');
      SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
          .showSidebar(html);
    }

你还可以复制这个Google表格,以获得可以用作基础的实际示例,请注意,该代码是GET请求,仅旨在说明其工作方式,似乎你需要的是POST请求,你可以使用此指南

希望这对你有所帮助!

英文:

Based on the description I would assume that in order to receive a response from the server the URL fetch service is being used, if so, the API documentation states that the requests will always come from a specific set of IPs, this means that from Apps Script backend this is not possible but there is a workaround but it requires to

  1. Create a Sidebar
  2. Run a fetch() function (This runs in AJAX so it runs in the client's side)

The result will look something like this:

在Google Sheets中调用客户端IP的URL是否可能?

The HTML file is called index in this example and it looks like this:

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
      </head>
      <body>
        <input type="button" onclick="fetching()"value="clickme"></input>
        </br>
        <div id="field"></div>
        <script>
        function fetching(){
        fetch('https://httpbin.org/anything')
        .then(response => response.json()) 
        //.then(json => console.log(json))
        .then(json => document.getElementById("field").innerHTML= "Response: "+JSON.stringify(json.origin))
         }
      </script>
      </body>
      
    
    </html>

And the code file looks like this:

    function onOpen() {
      SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
          .createMenu('Custom Menu')
          .addItem('Show sidebar', 'showSidebar')
          .addToUi();
    }
    
    function showSidebar() {
      var html = HtmlService.createHtmlOutputFromFile('index.html')
          .setTitle('My custom sidebar');
      SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
          .showSidebar(html);
    }

You could also copy this Google sheet to have a working example that you can use as a base, please keep in mind that the code is a GET request and is only meant to illustrate the way this works, it seems like what you would need is a POST request, to do that you can use this guide

Hope this helps!

huangapple
  • 本文由 发表于 2023年4月17日 21:11:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035541-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定