Hi there. I'm having some trouble getting a roURLTransfer to send back data. Specifically, I'm trying to get calendar events from a Google calendar using http GET. When I copy and paste the URL into a web browser, I get data, but when I use the roURLTranser in Brightscript, I get nothing back. I've tried both synchronously and asynchronously. This is what I'm doing:
calendarBaseURL = "https://www.googleapis.com/calendar/v3/calendars/{0}/events?key={1}&fields=items(id,summary,start,end)&timeMin={2}&timeMax={3}&singleEvents=true&orderBy=startTime"
calendarID = "jmtf3oba4r2b8ca0tuse9qbj3s@group.calendar.google.com"
// I have tried URL encoding the whole thing. Interestingly, the resulting URL does not even work in a web browser
// theURL = UrlEncode(substitute(calendarBaseURL, calendarID, calendarAPIKey, startTime.ToISOString(), endTime.ToISOString()))
// This URL has specific bits encoded as is done on the Google API documentation. It works in a web browser but not here.
theURL = substitute(calendarBaseURL, url_encode(calendarID), url_encode(calendarAPIKey), url_encode(startTime.ToISOString()), url_encode(endTime.ToISOString()))
print "the URL = ";theURL
// This is the synchronous version (commented out presently):
// scheduleRequest = CreateObject("roUrlTransfer")
// scheduleRequest.SetURL(theURL)
// theRawData = scheduleRequest.GetToString()
// print "theRawData = ";theRawData
// Now for the asynchronous approach:
xfer=createobject("roURLTransfer")
xfer.seturl(theURL)
xfer.SetRequest("GET")
print "the URL that is being used =";xfer.getURL()
port=createobject("roMessagePort")
xfer.setport(port)
timer=createobject("roTimeSpan")
timer.mark()
xfer.asyncgettostring()
while true
msg=wait(100,port) // 100 millisecond pause
if type(msg)="roUrlEvent" then
if msg.getresponsecode()=200 then
data=msg.getstring()
headers=msg.getresponseheadersarray()
exit while
else
xfer.asynccancel()
end if
else
// print "do something useful while we wait for data"
end if
if timer.totalmilliseconds() > 2000 then
?"timeout exceeded"
print "Failure Reason =";xfer.GetFailureReason()
exit while
end if
end while
When I run this, no data ever comes back. (FYI: I replaced all single-quote comments with "//" on here to make the auto syntax coloring not look weird.) Interestingly, the synchronous version does not stall out, but rather the line that reads:
print "theRawData = ";theRawData
returns nothing but an empty string.In the asynchronous version, a timeout happens but no failure reason is given.
The URL that is being sent looks like this:
https://www.googleapis.com/calendar/v3/calendars/jmtf3oba4r2b8ca0tuse9qbj3s%40group.calendar.google.com/events?key=[ MY API KEY REMOVED ]&fields=items(id,summary,start,end)&timeMin=2016-11-03T07%3A38%3A03Z&timeMax=2016-11-03T19%3A38%3A03Z&singleEvents=true&orderBy=startTime
Now I've removed my API key in order to post this on a public forum, but if I were to copy and paste the returned URL with my API key into a web browser, I get lots of glorious data without a problem.
I haven't set any headers. Could this be an issue? Or security issues with Google in some way? Any hints on what to look for?
Thanks,
Ron