Home > Mobile >  Google Script with Python example - GET Request - HTML result error
Google Script with Python example - GET Request - HTML result error

Time:11-13

I'm trying to connect a Google spreadsheet via API GET request to receive some information from snov.io, but I am getting a HTML result instead of a JSON. The below Python code works, but the Google Appscript not. Any suggestions? Thank you :)

Python code - Working

params = {
  'access_token': token,
  'domain': site,
  'type': 'all',
  'limit': 100,
  'lastId': 0,
  }
res = requests.get('https://api.snov.io/v2/domain-emails-with-info', params=params)
return json.loads(res.text)

AppScript - not working - received HTML result

  'access_token': token,
  'domain': site,
  'type': 'all',
  'limit': 100,
  'lastId': 0,
    }

var url = 'https://api.snov.io/v2/domain-emails-with-info'
var response = UrlFetchApp.fetch(url, {method: "get", payload: params, 'muteHttpExceptions': true}); 
var json = response.getContentText()
console.log(response.getContentText())
obj = JSON.parse(json)
result=obj.emails
return result
}

HTML Result :(

<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="robots" content="noindex,nofollow" />
        <style>                body { background-color: #fff; color: #222; font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; margin: 0; }
                .container { margin: 30px; max-width: 600px; }
                h1 { color: #dc3545; font-size: 24px; }</style>
    </head>
    <body>
                        <div class="container">
                    <h1>Whoops, looks like something went wrong.</h1>
                </div>
    </body>
</html>

CodePudding user response:

I believe your goal is as follows.

  • You want to convert the following python script to Google Apps Script.

      params = {
        'access_token': token,
        'domain': site,
        'type': 'all',
        'limit': 100,
        'lastId': 0,
        }
      res = requests.get('https://api.snov.io/v2/domain-emails-with-info', params=params)
      return json.loads(res.text)
    

In this case, how about the following modified script?

Modified script:

var params = {
  'access_token': token,
  'domain': site,
  'type': 'all',
  'limit': 100,
  'lastId': 0,
};
var url = 'https://api.snov.io/v2/domain-emails-with-info';
var response = UrlFetchApp.fetch(url   "?"   Object.entries(params).map(([k, v]) => k   "="   v).join("&"), { method: "get", 'muteHttpExceptions': true });
  • When params is used as the GET method at the request module of python, that is used as the query parameter. But, at the method of "UrlFetchApp.fetch" of Google Apps Script, when params is used to payload, it is automatically used as the POST method. I thought that this might be the reason for your issue.

Reference:

  • Related