0

I'm trying to get a response from a Java servlet and return it to a Gmail Add-On by using the following function:

function getResponse(){
  var url = "dgdespacho.nubbius.com/AddOn?command=searchRecords&clientName=&recordName=993&recordNo=&token=2671db95-3ece-40d9-842e-b573fc6a6ee6";
  var response = UrlFetchApp.fetch( url );
  Logger.log( response.getContentText() )
}

It should return a JSON-able string. When I make the request of the same url directly on a browser, it shows as it should:

{"data":[{"recordName":"prueba 993","id":5781921682423808}],"records":1,"status":0}

But inside the Add-On I always get an (incomplete) HTML text instead of a JSON:

<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
  <meta content="width=300, initial-scale=1" name="viewport">
  <meta name="google-site-verification" content="LrdTUW9psUAMbh4Ia074-BPEVmcpBxF6Gwf0MSgQXZs">
  <title>Sign in - Google Accounts</title>
  <style>
  @font-face { 
  [...]

I'm using the same structure that's shown in the documentation:

var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getContentText());

I've searched for similar questions (and primarily checked this one, as the other ones I've found don't really focus on the same topic) but they haven't solved this either.

What am I missing? How can I retrive with Apps Script what I retrieve when copying and pasting the same url on a web browser, if it's not using UrlFetchApp.fetch( url )?

  • 1
    The site you are trying to access needs a sign in from a google account. When you make that call from your browser, I suspect you are already signed in to your google account and have given access to the app previously. So it shows the expected json return value. Try opening the site in incognito or private mode, it should give you the login page. – Jack Brown Mar 01 '18 at 13:49
  • I am not sure what type of authorization the site uses, you can try this basic authorization as mentioned [here](https://stackoverflow.com/questions/23546255/how-to-use-urlfetchapp-with-credentials-google-scripts) – Jack Brown Mar 01 '18 at 13:54
  • Yes! That was it =D I had no Idea that it was requiring an authentication every time I made a request. I removed the url of my servlet from the authentication request filter (didn't even know it existed at first, I'm new in a several years old company and their code is already gigantic) and now it works perfectly =) Thank you! – Manuel Herrera Ojea Mar 07 '18 at 08:18

0 Answers0