Page 1 of 1

Getting info from URL into stack

Posted: Tue Sep 17, 2013 3:26 pm
by kellymdeters
I'm trying to get information from the Google Books API into a livecode stack. I've managed to get it into html format and if I go to this URL in a web browser I get exactly what I need (for example: put "http://kellymdeters.on-rev.com/readingt ... 0309217422"). However, when I try to get that info into my stack I keep getting the source, not the information as it would be viewed in a web browser.

I tried:

Code: Select all

put "http://kellymdeters.on-rev.com/readingtrackerapp/book.lc?isbn=9780309217422" into tURl
set the htmltext of field "field" to url tURL
but that still doesn't work.

Any ideas?

Thanks!



(This is the code that is in that book.lc page)

Code: Select all

<html>
<body>
    <div id="content"></div>
    <script>
      function handleResponse(response) {
      for (var i = 0; i < response.items.length; i++) {
        var item = response.items[i];
        document.getElementById("content").innerHTML += "<br>" + item.volumeInfo.title + "<br>" + item.volumeInfo.authors + "<br>" + item.volumeInfo.description + "<br>" + item.volumeInfo.pageCount + "<br>" + item.volumeInfo.imageLinks.smallThumbnail + "<br>" + item.volumeInfo.imageLinks.thumbnail;
      }
    }
    </script>
    <?lc
    put $_get["ISBN"] into tISBN
    put "<script src=" & quote & "https://www.googleapis.com/books/v1/volumes?q=isbn:" & tISBN & "&callback=handleResponse" & quote & "></script>"
    ?>
 
  </body>

</html>

Re: Getting info from URL into stack

Posted: Tue Sep 17, 2013 4:02 pm
by FourthWorld
The .lc page you have there relies on JavaScript to run the actual call to the Google APIs. Since LC has no JavaScript interpreter, the trick is to make that call yourself.

To try it I used:

Code: Select all

on mouseUp
   put "https://www.googleapis.com/books/v1/volumes?q=isbn:9780309217422" into tUrl 
   put url tURL into fld 1
   put the result
end mouseUp
Note the last line - "put the result". This is critical when diagnosing almost any seemingly-mysterious behavior in LiveCode, as "the result" will usually provide any error info if the preceding handler wasn't able to complete successfully (it'll be empty when successful).

In that case, "the result" contained:

Code: Select all

error -Error with certificate at depth: 2  issuer   = /C=US/O=Equifax/OU=Equifax Secure Certificate Authority  subject  = /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA  err 20:unable to get local issuer certificate
While seemingly cryptic, it provides some helpful tips, letting us know that the problem stems from not being able to resolve the certificate authority.

By default, when using https libURL will attempt to not only obtain the cert info needed to encrypt the connection, but will also verify that the domain and the certificate match. But many certs are used across multiple domains, and may not match the authority's record - and fortunately libURL provides a way to maintain the encryption without the domain authenticity check - libUrlSetSSLVerification:

Code: Select all

on mouseUp
   put "https://www.googleapis.com/books/v1/volumes?q=isbn:9780309217422" into tUrl 
   libUrlSetSSLVerification false
   put url tURL into fld 1
   put the result
end mouseUp
Once we turn off libURL's extra level of verification the data is returned as expected, apparently a JSON string.

The risk with turning off libUrlSetSSLVerification is that it exposes a system to the possibility of a man-in-the-middle attack. But since this is something that can only be addressed by the domain owner, in this case it's the best we can do (and apparently good enough for Google).

Re: Getting info from URL into stack

Posted: Tue Sep 17, 2013 4:32 pm
by kellymdeters
Thank you!!!

Re: Getting info from URL into stack

Posted: Fri Nov 01, 2013 9:48 pm
by atout66
Hi,

Could you tell me why a get an error message like that:
button "Button": compilation error at line 2 (Chunk: can't create a variable with that name (explicitVariables?)) near "tUrl", char 52
when I'm compiling my script placed into a button which is:

Code: Select all

on mouseUp
   put "this is supposed to be an internet adress but the forum prevent me to do it" into tUrl
   put url tURL into field "field"
   put the result
end mouseUp
quiet the same as you, no ?

Re: Getting info from URL into stack

Posted: Fri Nov 01, 2013 10:36 pm
by FourthWorld
In LiveCode's Preferences, see the "Script Editor" section, and turn off the "Strict Compilation Mode" option.

Re: Getting info from URL into stack

Posted: Fri Nov 01, 2013 10:43 pm
by atout66
Thanks for your help :)
I wouldn't have guess it...

Re: Getting info from URL into stack

Posted: Fri Nov 01, 2013 11:43 pm
by FourthWorld
It normally ships with that option turned off by default, so perhaps you'd turned it on while experimenting.

When it's turned on you need to declare all variables used in your script, and use quotes around all literal strings. See the Dictionary entry for the explicitVariables global property for details on that, since that's the property that's turned on momentarily during compilation to provide that extra level of error detection.