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: 
gzbruno
Visitor

Registration Sdk

Hello, hope you can help me out. Im trying to get the registration sdk example to work with my server, the first step is to test the server-channel communication, for this on the serverside i created a file named register.php. Which has the functions to generate a new token, and to check if the token is activated on the users table.
On the sdk side,ive only modified the first lines in regScreen.brs as follow: 
   m.UrlBase         = "http://www.mywebserver.com/roku/linking/register.php"
    m.UrlGetRegCode   = m.UrlBase + "?accion=getRegCode"
    m.UrlGetRegResult = m.UrlBase + "?accion=getRegResult"
    m.UrlWebSite      = "www.mywebserver.com"


and here its the script i have on register.php
<?
function random_string($length) {
    $key = '';
    $keys = array_merge(range(0, 9), range('a', 'z'));

    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }

    return $key;
}

$con=mysqli_connect("localhost",'user','pass',"db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  
  $serial = htmlspecialchars($_GET['deviceID']);
  $action = htmlspecialchars($_GET['accion']);
  
//Generate token
if($action == 'getRegCode'){

if(!empty($serial)){
//check if the device id is already on db
$query = "SELECT * FROM linking WHERE serial = '$serial'";
if ($result=mysqli_query($con,$query))
 {
 if(mysqli_num_rows($result) > 0){
 //if already exist, update the token at the device id
 $info = mysqli_fetch_array($result);
 $token = strtoupper(random_string(6));
 mysqli_query($con,"UPDATE linking SET token='$token' WHERE serial='$serial'");

 }else{
 //if new, make new record on db
 $token = strtoupper(random_string(6));
 mysqli_query($con,"INSERT INTO `linking` (`id`, `serial`, `token`, `used`) VALUES (NULL, '$serial', '$token', '0');");
 
 }
  

 }
 ?>
 <result>
 <status>success</status>
 <regCode><?=$token?></regCode>
 <retryInterval>30</retryInterval>
 <retryDuration>900</retryDuration>
</result>
 <?


}

//Now check if the token has been activated
}else if($action == 'getRegResult'){
 
$token = htmlspecialchars($_GET['regCode']);
/* structure
<linkAccount>
 <regCode>(current registration code from PreRegistration request)</regCode>
 <deviceID>(unique id/serial number for the device)</deviceID>
 <deviceTypeID>(opaque string identifying device type)</deviceTypeID>
</linkAccount>
*/

//check if token and device id are in db
$query = "SELECT * FROM users WHERE deviceid = '$serial' AND token='$token'";
if ($result=mysqli_query($con,$query))
 {
  if(mysqli_num_rows($result) > 0){
 echo "<result>
 <status>success</status>
 <deviceToken>".$token."</deviceToken>
 </result>";
  }else{
  echo "<result> <status>incomplete</status></result>";
  }
 }
}



?>


The first step works half way. Because if you enter the channel, you get the screen with the token, BUT if you click on "get a new code" button, it wont get a new code, and i check on the database and there is a new token updated at least once a second. But never show a new token on screen, also if you hit back, to the main app menu and try to get back in, it wont open any more, i have to exit the channel and enter again. actually this happens even if i dont click on get a new code button.

the second step is not working at all. i have tested the address on my web broswer directly .../register.php?accion=getRegResult&regCode=P84R83&deviceID=YU054L463584 and i get the following result:
<result><status>success</status><deviceToken>10SZ22</deviceToken></result>

So i assume the php script is working fine, but nothing happens at the channel, no message, no congratulations page, nothing.

Here is regScreen.brs
' *********************************************************
' **  Roku Registration Demonstration App
' **  Support routines
' **  May 2009
' **  Copyright (c) 2009 Roku Inc. All Rights Reserved.
' *********************************************************

'******************************************************
'Perform the registration flow
'
'Returns:
'    0 - We're registered. Proceed
'    1 - We're not registered. The user canceled the process.
'    2 - We're not registered. There was an error
'******************************************************

Function doRegistration() As Integer

   'xml responses are static, but there are a few flavors available for testing
   'Generic case: getRegResult (always returns success)
   'Failure case: getRegResult_failure (always returns failure)
   'Success case: getRegResult_success (always returns success)

    m.UrlBase         = "http://www.myserver.com/linking/register.php"
    m.UrlGetRegCode   = m.UrlBase + "?accion=getRegCode"
    m.UrlGetRegResult = m.UrlBase + "?accion=getRegResult"
    m.UrlWebSite      = "www.mywebserver.com"

    m.RegToken = loadRegistrationToken()
    if isLinked() then
        print "device already linked, skipping registration process" 
        'return 0
    endif

    regscreen = displayRegistrationScreen()

    'main loop get a new registration code, display it and check to see if its been linked
    while true
        
        duration = 0

        sn = GetDeviceESN() 
        regCode = getRegistrationCode(sn)

        'if we've failed to get the registration code, bail out, otherwise we'll
        'get rid of the retreiving... text and replace it with the real code       
        if regCode = "" then return 2
        regscreen.SetRegistrationCode(regCode)
        print "Enter registration code " + regCode + " at " + m.UrlWebSite + " for " + sn

        'make an http request to see if the device has been registered on the backend
        while true
            sleep(5000) ' to simulate going to computer and typing in regcode
                
            status = checkRegistrationStatus(sn, regCode)
            if status < 3 return status

            getNewCode = false
            retryInterval = getRetryInterval()
            retryDuration = getRetryDuration()
            print "retry duration "; itostr(duration); " at ";  itostr(retryInterval);
            print " sec intervals for "; itostr(retryDuration); " secs max"
          
            'wait for the retry interval to expire or the user to press a button
            'indicating they either want to quit or fetch a new registration code
            while true
                print "Wait for " + itostr(retryInterval)
                msg = wait(retryInterval * 1000, regscreen.GetMessagePort())
                duration = duration + retryInterval
                if msg = invalid exit while

                if type(msg) = "roCodeRegistrationScreenEvent"
                    if msg.isScreenClosed()
                        print "Screen closed"
                        return 1
                    elseif msg.isButtonPressed()
                        print "Button pressed: "; msg.GetIndex(); " " msg.GetData()
                        if msg.GetIndex() = 0
                            regscreen.SetRegistrationCode("retrieving code...")
                            getNewCode = true
                            exit while
                        endif
                        if msg.GetIndex() = 1 return 1
                    endif
                endif
            end while
            
            if duration > retryDuration exit while
            if getNewCode exit while

            print "poll prelink again..."
        end while
    end while

End Function


'********************************************************************
'** display the registration screen in its initial state with the
'** text "retreiving..." shown.  We'll get the code and replace it
'** in the next step after we have something onscreen for teh user 
'********************************************************************
Function displayRegistrationScreen() As Object

    regsite   = "go to " + m.UrlWebsite
    regscreen = CreateObject("roCodeRegistrationScreen")
    regscreen.SetMessagePort(CreateObject("roMessagePort"))

    regscreen.SetTitle("")
    regscreen.AddParagraph("Please link your Roku player to your account by visiting")
    regscreen.AddFocalText(" ", "spacing-dense")
    regscreen.AddFocalText("From your computer,", "spacing-dense")
    regscreen.AddFocalText(regsite, "spacing-dense")
    regscreen.AddFocalText("and enter this code to activate:", "spacing-dense")
    regscreen.SetRegistrationCode("retrieving code...")
    regscreen.AddParagraph("This screen will automatically update as soon as your activation completes")
    regscreen.AddButton(0, "Get a new code")
    regscreen.AddButton(1, "Back")
    regscreen.Show()

    return regscreen

End Function


'********************************************************************
'** Fetch the prelink code from the registration service. return
'** valid registration code on success or an empty string on failure
'********************************************************************
Function getRegistrationCode(sn As String) As String

    if sn = "" then return ""

    http = NewHttp(m.UrlGetRegCode)
    http.AddParam("partner", "roku")
    http.AddParam("deviceID", sn)
    http.AddParam("deviceTypeName", "roku")

    rsp = http.Http.GetToString()
    xml = CreateObject("roXMLElement")
    print "GOT: " + rsp
    print "Reason: " + http.Http.GetFailureReason()

    if not xml.Parse(rsp) then
        print "Can't parse getRegistrationCode response"
        ShowConnectionFailed()
        return ""
    endif

    if xml.GetName() <> "result"
        Dbg("Bad register response: ",  xml.GetName())
        ShowConnectionFailed()
        return ""
    endif

    if islist(xml.GetBody()) = false then
        Dbg("No registration information available")
        ShowConnectionFailed()
        return ""
    endif

    'default values for retry logic
    retryInterval = 30  'seconds
    retryDuration = 900 'seconds (aka 15 minutes)
    regCode       = ""

    'handle validation of response fields 
    for each e in xml.GetBody()
        if e.GetName() = "regCode" then
            regCode = e.GetBody()  'enter this code at website
        elseif e.GetName() = "retryInterval" then
            retryInterval = strtoi(e.GetBody())
        elseif e.GetName() = "retryDuration" then
            retryDuration = strtoi(e.GetBody())
        endif
    next

    if regCode = "" then
        Dbg("Parse yields empty registration code")
        ShowConnectionFailed()
    endif

    m.retryDuration = retryDuration
    m.retryInterval = retryInterval
    m.regCode = regCode

    return regCode

End Function


'******************************************************************
'** Check the status of the registration to see if we've linked
'** Returns:
'**     0 - We're registered. Proceed.
'**     1 - Reserved. Used by calling function.
'**     2 - We're not registered. There was an error, abort.
'**     3 - We're not registered. Keep trying.
'******************************************************************
Function checkRegistrationStatus(sn As String, regCode As String) As Integer

    http = NewHttp(m.UrlGetRegResult)

    http.AddParam("partner", "roku")
    http.AddParam("deviceID", sn)
    http.AddParam("regCode", regCode)

    print "checking registration status"

    while true
        rsp = http.Http.GetToString()
        xml = CreateObject("roXMLElement")
        if not xml.Parse(rsp) then
            print "Can't parse check registration status response"
            ShowConnectionFailed()
            return 2
        endif

        if xml.GetName() <> "result" then
            print "unexpected check registration status response: ", xml.GetName()
            ShowConnectionFailed()
            return 2
        endif

        if islist(xml.GetBody()) = true then
            for each e in xml.GetBody()
                if e.GetName() = "regToken" then
                    token = e.GetBody()

                    if token <> "" and token <> invalid then
                        print "obtained registration token: " + validstr(token)
                        saveRegistrationToken(token) 'commit it
                        m.RegistrationToken = token
                        showCongratulationsScreen()
                        return 0
                    else
                        return 3
                    endif
                elseif e.GetName() = "customerId" then
                    customerId = strtoi(e.GetBody())
                elseif e.GetName() = "creationTime" then
                    creationTime = strtoi(e.GetBody())
                endif
            next
        endif
    end while

    print "result: " + validstr(regToken) +  " for " + validstr(customerId) + " at " + validstr(creationTime) 

    return 3

End Function


'***************************************************************
' The retryInterval is used to control how often we retry and
' check for registration success. its generally sent by the
' service and if this hasn't been done, we just return defaults 
'***************************************************************
Function getRetryInterval() As Integer
    if m.retryInterval < 1 then m.retryInterval = 30
    return m.retryInterval
End Function


'**************************************************************
' The retryDuration is used to control how long we attempt to 
' retry. this value is generally obtained from the service
' if this hasn't yet been done, we just return the defaults 
'**************************************************************
Function getRetryDuration() As Integer
    if m.retryDuration < 1 then m.retryDuration = 900
    return m.retryDuration
End Function


'******************************************************
'Load/Save RegistrationToken to registry
'******************************************************

Function loadRegistrationToken() As dynamic
    m.RegToken =  RegRead("RegToken", "Authentication")
    if m.RegToken = invalid then m.RegToken = ""
    return m.RegToken 
End Function

Sub saveRegistrationToken(token As String)
    RegWrite("RegToken", token, "Authentication")
End Sub

Sub deleteRegistrationToken()
    RegDelete("RegToken", "Authentication")
    m.RegToken = ""
End Sub

Function isLinked() As Dynamic
    if Len(m.RegToken) > 0  then return true
    return false
End Function

'******************************************************
'Show congratulations screen
'******************************************************
Sub showCongratulationsScreen()
    port = CreateObject("roMessagePort")
    screen = CreateObject("roParagraphScreen")
    screen.SetMessagePort(port)

    screen.AddHeaderText("Congratulations!")
    screen.AddParagraph("You have successfully linked your Roku player to your account")
    screen.AddParagraph("Select 'start' to begin.")
    screen.AddButton(1, "start")
    screen.Show()

    while true
        msg = wait(0, screen.GetMessagePort())

        if type(msg) = "roParagraphScreenEvent"
            if msg.isScreenClosed()
                print "Screen closed"
                exit while                
            else if msg.isButtonPressed()
                print "Button pressed: "; msg.GetIndex(); " " msg.GetData()
                exit while
            else
                print "Unknown event: "; msg.GetType(); " msg: "; msg.GetMessage()
                exit while
            endif
        endif
    end while
End Sub



I would really appreciate the help as im stuck here for almost 2 weeks. What is wrong with either codes? or anybody has a more complete channel pack for example?
Thanks in advance.
0 Kudos
13 REPLIES 13
gzbruno
Visitor

Re: Registration Sdk

i got it half way working by changing <deviceToken> o <regToken> in the xml. now when i update the database it almost automatically show the congratulations screen. Now I have 2 more problems:

When i hit the start button, it takes me to the channel home, but when i click any option, im being headed again to the token site.
I think maybe here is the problem 
Function showHomeScreen(screen) As Integer

    if type(screen)<>"roPosterScreen" then
        print "illegal type/value for screen passed to showHomeScreen"
        return -1
    end if

    itemNames = getItemNames()
    screen.SetContentList(itemNames)
    screen.Show()

    while true
        msg = wait(0, screen.GetMessagePort())

        if type(msg) = "roPosterScreenEvent" then
            print "showHomeScreen | msg = " +  msg.GetMessage() + " | index = " + itostr(msg.GetIndex())

            if msg.isListItemSelected() then
                doRegistration()
            else if msg.isScreenClosed() then
                return -1
            end if
        end If
    end while

End Function

As any item in the list points directly to registration, there is no other thing the demo does. But also, why is the registration token shown again if the device is supposed to be already linked?

Now i want to integrate the video player sdk to have multiple categories and a video player, how can i accomplish this?
0 Kudos
RokuMarkn
Visitor

Re: Registration Sdk

Just a guess, I haven't studied your code in detail, but at the start of doRegistration you have a statement that says

   if isLinked() then
       print "device already linked, skipping registration process"
       'return 0
   endif

The return statement is commented out, so this does nothing.  Perhaps you should remove the quote at the beginning of the return.

--Mark
0 Kudos
gzbruno
Visitor

Re: Registration Sdk

Thank you Markn

I removed the ' and now it wont show the registration screen, but im not quite sure it works as desired, because if i edit the field on my database, it wont disable the player, even if i close and open again the app. by theory if i modify the database info, the php code wont find a match and will deploy a failure or incomplete xml. but its not happening, i mean the php code is showing the incomplete or failure, but the app isnt taking any actions.

Also I want to integrate this to a sdk where it could have multiple categories and read from xml, but ive done a vast search on the forums and google and couldnt find an example. 

at the actual example, which i think is a dummy. i have the following way to call the doRegistration function
Function showHomeScreen(screen) As Integer

    if type(screen)<>"roPosterScreen" then
        print "illegal type/value for screen passed to showHomeScreen"
        return -1
    end if

    itemNames = getItemNames()
    screen.SetContentList(itemNames)
    screen.Show()

    while true
        msg = wait(0, screen.GetMessagePort())

        if type(msg) = "roPosterScreenEvent" then
            print "showHomeScreen | msg = " +  msg.GetMessage() + " | index = " + itostr(msg.GetIndex())

            if msg.isListItemSelected() then
                doRegistration()
            else if msg.isScreenClosed() then
                return -1
            end if
        end If
    end while

End Function


If i want the registration screen to open when the app opens where should i put the doRegistration() function? ive tried on appmain

Sub Main()
 doRegistration()
    'initialize theme attributes like titles, logos and overhang color
    initTheme()

    'prepare the screen for display and get ready to begin
    screen=preShowHomeScreen("Register", "")
    if screen=invalid then
        print "unexpected error in preShowHomeScreen"
        return
    end if

    showHomeScreen(screen)

End Sub

but after you finish the registration, instead of opening the app or loading the home screen, it just kicks you out of the channel.
0 Kudos
jhoya
Visitor

Re: Registration Sdk

Where did you find regScreen.brs from 2009? I've been working on the auth-linking-channel from 2016. Find it at https://github.com/rokudev/sample-channels/raw/master/auth-linking-channel.zip
0 Kudos
jhoya
Visitor

Re: Registration Sdk

And another thing: roCodeRegistrationScreen (in line 106 of your regScreen.brs) is deprecated
https://sdkdocs.roku.com/display/sdkdoc/roCodeRegistrationScreen
0 Kudos
gzbruno
Visitor

Re: Registration Sdk

"jhoya" wrote:
Where did you find regScreen.brs from 2009? I've been working on the auth-linking-channel from 2016. Find it at https://github.com/rokudev/sample-channels/raw/master/auth-linking-channel.zip

As you can notice im lost, im looking in wrong places. 
Ive seen the 2016 version, but i dont understand how it works. At the 2009 script, it connects to the server using get method and expect an xml response. I think and correct me if im wrong, the new script connects using json? 
  m.gen = "http://www.bgb.mx/roku/linking2/test.php?token=" + m.rokuDeviceID
  m.auth = "http://rokuleow.pythonanywhere.com/authenticate?token=" + m.rokuDeviceID
  m.dis = "http://rokuleow.pythonanywhere.com/disconnect?token=" + m.rokuDeviceID


Do you have a sample in php of these files? how to handle the client request and sending back the response?
0 Kudos
jhoya
Visitor

Re: Registration Sdk

gzbruno, 

First, I strongly recommend that you drop that 2009 file because it will be deprecated, and start using the 2016 version because it works with SceneGraph.

Second, yes, I have php files to:

1. generate: take the token from the roku device (token = serial number + model number) and generate a six-digit (alphanumeric) code to display on the roku device screen;  return as json code, token and update
2. authenticate: take the token, generate a 12-digit alphanumeric code for the oauth_token which is written to device registry and database; return as json token, linked status, and oauth_token 
3. activate: take user_number and that 6-digit code from generate.php as input from a html form; check a subscriber list that the user_number is a subscriber (there's a limit of 2 devices per subscriber account) and that the 6-digit code matches

...and then success! If the subscriber wasn't linked before, she is now; if she was, she goes right to the content scene.

How's your php? Here's the first one, generate.php


<?php header('content-type: application/json; charset=utf-8');
    // GENERATE

if (isset($_GET["token"])) {
require_once "../../settings.php";
    require_once "../../utils.php";

    $token = $_GET["token"];
    $update_gen = "yes";

// First, check if it has an agent associated with it... 0 or 1 is okay, 2 is a too many

    $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
    if ($mysqli->connect_error) {
        die("Connection failed: " . $mysqli->connect_error);
    }
    $query = "SELECT linked FROM `<my_database>` WHERE token = ?";
    if ($stmt = $mysqli->prepare($query)) {
        $stmt->bind_param('s', $token);
        $stmt->execute();
        $stmt->bind_result($linked);
        $stmt->fetch();
        $stmt->close();
    } else {
        echo "Errormessage1: " . $mysqli->error;
    }   
    $mysqli->close();


// Second, if linked = 'yes', 

    if ($linked) {
        //it's linked; read value for code
        $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
        if ($mysqli->connect_error) {
            die("Connection failed: " . $mysqli->connect_error);
        }
        $query = "SELECT code FROM `<my_database>` WHERE token = ?";
        if ($stmt = $mysqli->prepare($query)){
            $stmt->bind_param('s', $token);
            $stmt->execute();
            $stmt->bind_result($code);
            $stmt->fetch();           
            $stmt->close();  
            $update_gen = "no";
        } else {
            die("Errormessage2: ");
        }    
        
    } else {
        // a new entry!!! device not linked yet
        // generate code and write token, code, update_gen and linked to db
        $code = makeCode(6);
        $linked = "no";
        $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
        if ($mysqli->connect_error) {
            die("Connection failed: " . $mysqli->connect_error);
        }
        $query = "INSERT INTO `<my_database>` (token, code, update_gen, linked) VALUES (?, ?, ?, ?) ";
        if ($stmt = $mysqli->prepare($query)){
            $stmt->bind_param('ssss',  $token, $code, $update_gen, $linked);
            $stmt->execute();
            $stmt->close();
        } else {
            die("Errormessage3: ". $mysqli->error);
        }
    }

    $mysqli->close();

    
    $d = array("code" => $code, "deviceToken" => $token, "update" => $update_gen);
$result = json_encode($d);

    echo $result;

}
?>


settings.php has the host, user, password and database names; utils.php has some functions to sanitize input and generate codes, like makeCode(length) below


// generate a random base36 code; length is argument
function makeCode($code_length){
    $text = "";
    $possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // base36
    
    for( $i=0; $i < $code_length; $i++ ){
        $randomness = rand(0,35);
        $text = $text . $possible{$randomness};
    }
    
    return $text;
  }


Let me know if this will work for you. It may not be the best or most efficient php, but it works (kinda like php itself).

Good luck.
0 Kudos
destruk
Streaming Star

Re: Registration Sdk

Using the serial number in this type of method is frowned upon by roku and introduces a security hole.  If the user sells or gives away their roku box to someone else, that person will then have access to their account on your server even after a factory reset of the device because you are using the serial number + model number to determine their account information.  It's better to randomly generate a token, check to make sure it doesn't exist in your database already, and use that to link with.
0 Kudos
jhoya
Visitor

Re: Registration Sdk

Thanks, destruk. You're right, but in our case we have the additional security layer of subscriber number. This app is for a private channel whose subscribers are franchisees who must renew quarterly.

But if the serial number is frowned upon by Roku, why is that used in SimpleScene.brs in auth-linking-channel.zip from the Roku github? Specifically:

m.rokuDeviceID = deviceInfo.GetDeviceUniqueId() + m.model
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.