How to get json data from a REST API call?

Hi

I have been trying to gather some data from a different server for which I can use REST API to get datas.
I am trying to use roURLTransfer and its events but have no success yet and always getting

BRIGHTSCRIPT: ERROR: ParseJSON: Data is empty:

My code is as below:


    request=CreateObject("roUrlTransfer")       
    request.SetURL("https://apis.someserver.com/glist")    
    request.AddHeader("Authorization", "Basic")    
    request.AddHeader("app_key","username")
    request.AddHeader("app_secret","password")
    response = ParseJson(request.GetToString())

Can anyone point out what am I doing wrong and what is the right way to connect to some server with given api-keys.

Thanks

If the server is using TLS/SSL, then you need to call: request.SetCertificatesFile (“common:/certs/ca-bundle.crt”)

Also, don’t pass request.GetToString() into ParseJson(). Check the HTTP error code first.

function AuthSerial()

  serial = GetDeviceESN()
  request = CreateObject("roUrlTransfer")
  ba = CreateObject("roByteArray") 
  ba.FromAsciiString(serial) 
  request.SetCertificatesFile("pkg:/certs/certs.crt")
  request.InitClientCertificates()
  request.AddHeader("X-Content", ba.ToBase64String())   
  request.SetUrl("https://apis.someserver.com/dato.php")
  html = request.GetToString()
  data = html.Trim() 
  json = ParseJSON(data)
  return json
  
end function

  dato= AuthSerial()

  user = dato.user
  pass = dato.pass

BRIGHTSCRIPT: ERROR: ParseJSON: Data is empty

You need to debug your code to view more details about errors. It might be some https issues with security check.

Take a look: http://forums.roku.com/viewtopic.php?f=34&t=92989

“dreamer2057” wrote:
You need to debug your code to view more details about errors. It might be some https issues with security check.

Take a look: http://forums.roku.com/viewtopic.php?f=34&t=92989

and try everything

“belltown” wrote:
Another thing that might help you is to call event.GetFailureReason() to get a more descriptive reason for the error.

“dreamer2057” wrote:

dd = event.GetString()
code = event.GetResponseCode()

and ‘print’ them

For example..

“dreamer2057” wrote:

“belltown” wrote:
Another thing that might help you is to call event.GetFailureReason() to get a more descriptive reason for the error.

“dreamer2057” wrote:

dd = event.GetString()
code = event.GetResponseCode()

and ‘print’ them

For example..

I do not understand what you mean

Something like this:

 

Function TestHttps() as Dynamic
    print "HTTPS STARTED"
    
    url = " ... enter url "
    timeout = 1500
    num_retries = 1
    
    http = CreateObject("roUrlTransfer")
    port = CreateObject("roMessagePort")
    
    http.SetUrl(url)
    http.SetMessagePort(port)
    http.SetRequest("POST")
    http.AddHeader("Content-Type", "application/json")
    http.AddHeader("X-Roku-Reserved-Dev-Id")
    http.SetCertificatesFile("pkg:/source/certificates/cafile.pem")
    http.EnableHostVerification(true)
    http.EnablePeerVerification(true)
    http.InitClientCertificates()
    http.RetainBodyOnError(true)
    
    while num_retries > 0
        if (http.AsyncPostFromString(""))
            event = wait(timeout, http.GetPort())
            print event
            if type(event) = "roUrlEvent"
                dd =  event.GetString()
                code = event.GetResponseCode()
                print code
                if code = 200
                    print "HTTPS data:" + dd
                    print "HTTPS code:" + ToString(code)
                    return dd
                else
                    print "HTTPS data:" + dd
                    print "HTTPS code:" + ToString(code)
                    timeout = timeout * 2
                    num_retries = num_retries - 1
                    print "HTTPS New try: " + ToString(num_retries)
                endif
            else
                timeout = timeout * 2
                num_retries = num_retries - 1
                print "HTTPS New try: " + ToString(num_retries)
            endif
        endif
    end while
    print "HTTPS FINISHED"
End Function

http.SetCertificatesFile("pkg:/source/certificates/cafile.pem")

It needed if you have ca certificate file that makes domain trusted from untrusted, otherwise

http.SetCertificatesFile ("common:/certs/ca-bundle.crt")

If all will be good: code will be 200 and data will be in correct format (valid json), then need continue to thinking

and how to work with my server certificate

Hi Guys

I am still having the same problem

-77
error setting certificate verify locations:
  CAfile: /tmp/plugin/BEAAAA8IJGIU/pkg:/certs/cacert.crt
  CApath: none

Also checked this forum link [http://forums.roku.com/viewtopic.php?f=34&t=39683].
May be certificate file is the issue. Where would I get the CA certs file from. I have a cacert.pem file from the ROKU SDK and used openssl to convert it to cacert.crt and used this in the code as well. But still getting the error.

Below is my code, which I have been currently testing by placing it in the Main function. I am trying to get the list of items from the server using the provided server API with its username and password. Any help would be much appreciated.


    http = CreateObject("roUrlTransfer")
    port = CreateObject("roMessagePort")
    http.SetUrl("https://someserver/playlist")
    http.SetMessagePort(port)
    ba = CreateObject("roByteArray") 
    ba.FromAsciiString("username:password")
    http.AddHeader("Authorization", "Basic " + ba.ToBase64String())
    http.AddHeader("Content-Type", "application/json")
    'http.AddHeader("X-Roku-Reserved-Dev-Id", "")
    http.SetCertificatesFile("pkg:/certs/cacert.crt")
    http.InitClientCertificates()
    http.RetainBodyOnError(true)

    num_tries = 1
    while num_tries > 0
        if (http.AsyncGetToString()) 'AsyncPostFromString("")
            event = wait(0, http.GetPort())
            if type(event) = "roUrlEvent"
                dd =  event.GetString()
                code = event.GetResponseCode()
                st = event.GetFailureReason()
                print code; "--------"
                print st; "&&&&&&&&&&&"
                num_tries = num_tries -1
            end if
        end if
    end while

Check file location, Roku cannot get data from it.
Look at code errors:
https://sdkdocs.roku.com/display/sdkdoc/roUrlEvent

Thanks.

Yes the file location is correct. The file is also there in zip file. But still the same error.

I am testing in below ROKU device using my Eclipse IDE


Model: 3500X - Roku Stick
Software Version: Version 7.0 . build 9044

Let me again specify my steps here:

Copied the cacert.pem file from the ROKU SDK

Used locally installed openssl to grab the cacert.crt file using the pem file

Copied the crt file into the server’s chain crt file

cacert.crt file is copied in the project folder and used as the above mentioned code

Surely, app is not being able to load the crt file. Even when deleting the file from zip before installing it in to the Roku stick, I get the same error.

CAfile: /tmp/plugin/BEAAAA8IJGIU/pkg:/certs/cacert.crt

I am running out of ideas, is there any other way to check how it should work.

What certificate authority is the server’s certificate signed with?

Does the server need to verify the Roku’s client certificate?

Server uses GoDaddy Standard UCC SSL.

“ov2015” wrote:
Server uses GoDaddy Standard UCC SSL.
Did they give you an intermediate certificate to install on your server? If so, try copying that to your Roku package and using that in the call to SetCertificatesFile().

“belltown” wrote:

“ov2015” wrote:
Server uses GoDaddy Standard UCC SSL.
Did they give you an intermediate certificate to install on your server? If so, try copying that to your Roku package and using that in the call to SetCertificatesFile().

I downloaded the GoDaddy Certificate Bundles - G2 from here: https://certs.godaddy.com/repository/.
Used it in the SetCertificatesFile call and I am receiving 200 status. :grinning_face_with_smiling_eyes:
Thanks for your help. Much Appreciated.