mrfrerichs
mrfrerichs

Reputation: 97

Retrieve Public IP From Google Web App

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?

Upvotes: 6

Views: 8905

Answers (3)

Shivam Sahil
Shivam Sahil

Reputation: 4921

Though you directly just can't get the IP Address of any client via app script as it has to pass through Google proxies but there's a better way to get it via some open geolocation apis.

When using an open geolocation APIs, the client sends a request to these APIs which then returns a response about all the ip and location details of that client which can be pushed to your appscript backend in form of JSON payload.

Here's how I did it using Axios:

const getData = async () => {
            axios.get("https://geolocation-db.com/json/").then(response => {
                axios({
                    method: "post",
                    url: process.env.REACT_APP_SHEETS_URI,
                    data: {
                        apiKey: process.env.REACT_APP_SHEETS_API_KEY,
                        operationType: process.env.REACT_APP_UPDATE_WEBSITE_PING,
                        operationData: response.data,
                    },
                    headers: {
                        "Content-Type": "text/plain;charset=utf-8",
                    },
                })
                    .then(function () {})
                    .catch(function (error) {
                        console.log("Error Occured = ", error);
                    });
            });
        };

And here's my appscript code to handle the request:

const doPost = (request = {}) => {
  const { _, postData: { contents, type } = {} } = request;
  let query = {};
  if (type === 'application/json') {
    query = JSON.parse(contents);

  }

  else if (type === 'application/x-www-form-urlencoded') {
    
    contents
      .split('&')
      .map((input) => input.split('='))
      .forEach(([key, value]) => {
        query[decodeURIComponent(key)] = decodeURIComponent(value);

      });
  }
  else if(type === "text/plain")
  {
    let jsonContent = "Error, Not Able to Parse";
  try {
    query = JSON.parse(contents);
  }

  catch(e) {
  return ContentService.createTextOutput(JSON.stringify(
                                          {
                                            error: true, 
                                            request: "Invalid JSON Request",
                                            msg: "Unable to Parse JSON",
                                            type: type,
                                            requestBody: contents,
                                            requestBodyDecoded: jsonContent

                                        })).setMimeType(ContentService.MimeType.JSON);
  }
  }
  else
  return ContentService.createTextOutput(JSON.stringify(
                                          {
                                            error: true, 
                                            request: "Invalid Request: Can't Identify the Type",
                                            msg: "Unknown Request Type",
                                            type: type

                                        })).setMimeType(ContentService.MimeType.JSON);



  const operationType = query.operationType;

    if(operationType === undefined)
    return ContentService.createTextOutput(JSON.stringify(
    {
      error: false, 
      queryOpsType: "undefined"
  })).setMimeType(ContentService.MimeType.JSON);

  else
  {
    const operationData = query.operationData;
    const response = handleRequestBasedonOperation(operationType,operationData);
    return ContentService.createTextOutput(JSON.stringify(
    {
      error: false, 
      msg: response.msg,
      fullResponse: JSON.stringify(response),
      queryOpsType: query.operationType
  })).setMimeType(ContentService.MimeType.JSON);
  }
  }

  else

  return ContentService.createTextOutput(JSON.stringify(
    {
      data: isAuthenticated.data,
      error: true, 
      //request: request,
      msg: query.apiKey,
      //paramters:request.parameters
  })).setMimeType(ContentService.MimeType.JSON);

where handleRequestBasedonOperation handles the operation, you may choose to dump the data into a googleSheet or anywhere else you would like to.

Upvotes: 0

Riyafa Abdul Hameed
Riyafa Abdul Hameed

Reputation: 7973

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');
}

Upvotes: 2

Mogsdad
Mogsdad

Reputation: 45720

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.

Upvotes: 8

Related Questions