Retrieve Public IP From Google Web App

2019-05-15 01:40发布

The Google Scripts documentation does not describe any way to retrieve the client's IP address from a Google Apps Script published as a Web App.

Can that be done?

2条回答
再贱就再见
2楼-- · 2019-05-15 02:15

Client access to published Web Apps is channeled through Google's Proxies, so any attempt to get a client's IP will instead report the proxy's IP.

There are no Service APIs that provide a client's IP, but we can use external javascript libraries through the HTML service.

Here is some demonstration code adapted from How to get client's IP address using javascript only?

Code.gs

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('getIp');
}

getIp.html

<script type="application/javascript">
  function getip(json){
    alert(json.ip); // alerts the ip address
  }
</script>
<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>

Result

In one test, the alert popped up with 216.191.234.70. Lookup of that IP:

Screenshot

That's definitely not my IP address.

Conclusion: No, you can't retrieve a user's public IP address using Google Script.

查看更多
三岁会撩人
3楼-- · 2019-05-15 02:21

The following code will return user's ip:

getIp.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">

  </head>
  <body>

<p id="ip"></p>
 <script type="text/javascript">
  var userip;
</script>

<script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>

<script type="text/javascript">
  document.write("Your IP is :", userip);
</script>
  </body>
</html>

Code.js

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('getIp');
}
查看更多
登录 后发表回答