Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
tifroz
Streaming Star

Can't get AddCookie() to work (ifHttpAgent)

I can't seem to be able to get cookies working on an roVideoPlayer object (implements ifHttpAgent). I have mocked up some really simple code to try to narrow it down, but even my stripped down code doesn't work as expected. Does anyone have a working example of AddCookie(), or some pointers to share?


Here's what I have tried:



player = CreateObject("roVideoPlayer")
expires = CreateObject("roDateTime")
expires.FromSeconds( expires.asSeconds() + 3600 )

' Using an empty string domain (If domain is an empty string, all domains are matched)
player.AddCookies([{version:1, name:"thecookie", value:"the value", domain: "", path:"/", expires: expires}])

' This should print an roArray containing one roAssociativeArray element - instead it prints an empty roArray
Print "player.getCookies(): " player.getCookies(".google.com", "/")

0 Kudos
5 REPLIES 5
belltown
Roku Guru

Re: Can't get AddCookie() to work (ifHttpAgent)

Make sure you call player.EnableCookies() first.

Also, AddCookies() has a Boolean return code. It returns False if the call fails, e.g. because you haven't called EnableCookies().
0 Kudos
tifroz
Streaming Star

Re: Can't get AddCookie() to work (ifHttpAgent)

Thanks - I tried again after inserting player.EnableCookies() before adding cookies ... it did affect the boolean value returned by AddCookie() (true if cookies are enabled, false otherwise), but GetCookies() is still returning empty.

I don't think the behavior is caused by a flawed call to GetCookies(): I tested the same logic against a server while monitoring http traffic and the cookie header isn't showing there either
0 Kudos
belltown
Roku Guru

Re: Can't get AddCookie() to work (ifHttpAgent)

Do this return anything: player.getCookies("", "/")

I was also wondering whether you had the domain correct ".google.com" perhaps should be "google.com"
0 Kudos
Veeta
Visitor

Re: Can't get AddCookie() to work (ifHttpAgent)

Here's my general flow for adding previously stored cookies to a new roUrlTransfer object:



' Getting some useful cookies
Function login(hostname As String)
http = CreateObject("roUrlTransfer")
http.SetUrl("http://" + hostname + "/...")
http.EnableCookies()
http.AsyncPostFromString(...)
'... wait for response
http.GetCookies(hostname, "/")
End Function

' Using those cookies later
Function usecookies(hostname As String)
http = CreateObject("roUrlTransfer")
http.SetUrl("http://" + hostname + "/...")
http.EnableCookies()
http.AddCookies(cookies)
http.AsyncGetToString()
...
End Function


This works for me after a little trial and error. I believe the key is to make sure the hostname lines up everywhere. If you are setting a cookie for an empty domain, it may ignore it or at least not return it when you ask for cookies from a different domain. Cookie domain matching is a security-sensitive subject. Another hint is that we know it's libcurl under the covers. Looking at curl's documentation on cookie management might give more insight.
tifroz
Streaming Star

Re: Can't get AddCookie() to work (ifHttpAgent)

OK thanks for all the help. I got it to work with 2 changes overall
• EnableCookie() doc says "Causes any Set-Cookie headers returned from the request to be interpreted and the resulting cookies to be added to the cookie cache" - but it turns out to be necessary also when adding cookies manually via AddCookie()
• Empty domain names make sense for GetCookies(), but don't make sense for AddCookies()

Here is a working version of the code



expires = CreateObject("roDateTime")
expires.FromSeconds( expires.asSeconds() + 3600 )
cookie = {version:1, name:"thecookies", value:"thevalue", domain: domain, path:path, expires: expires}

player = CreateObject("roVideoPlayer")
player.EnableCookies()
player.AddCookies([cookie])
player = http.GetCookies("", "/")
if cookies.count() = 1
Print "YEP"
else
Print "NOPE"
end if
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.