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: 
destruk
Binge Watcher

Re: Registration Sdk

It's probably used because their sample code is based on code a decade old, before they worried about such things. 🙂  However, if you use that in a public channel it will block certification and not pass QA.  As private channels don't go through QA you're fine, just keep it in mind.
0 Kudos
destruk
Binge Watcher

Re: Registration Sdk

"It's OK to use the SN to allow the user to identify a particular box on your site, but it should not be used to associate the box to an account in authorization.
The factory reset works properly to disassociate the box from Netflix, Amazon, MLB, and other major partners.... Rendezvous linking is very doable. 
--Kevin"


https://forums.roku.com/viewtopic.php?f ... al#p196862
0 Kudos
gzbruno
Visitor

Re: Registration Sdk

Thank you destruct, It might not be the best method but if it works it is ok for me. Or if you want to share the code for more secure it would be appreciated.

So i made just 1 file, generate.php here is the code.
<?php header('content-type: application/json; charset=utf-8');
    // GENERATE

if (isset($_GET["token"])) {
 $db_host = "localhost";
 $db_user = "user";
 $db_pass = "pass";
 $db_name = "db";
 

// 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;
  }
 
    $token = htmlspecialchars($_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 roku_prueba WHERE token = '$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 roku_prueba WHERE token = $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 roku_prueba (token, code, update_gen, linked) VALUES ('$token', '$code', '$update_gen', '$linked') ";
        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;

}
?>


And here is SimpleScreen.brs
' ********** Copyright 2016 Roku Corp.  All Rights Reserved. **********

' 1st function that runs for the scene component on channel startup
sub init()
  print "init scene"
  m.Button     = m.top.findNode("Button")
  m.Token      = m.top.findNode("Token")
  m.HowTo      = m.top.findNode("HowTo")
  m.Login      = m.top.findNode("Login")
  m.RegTask    = m.top.findNode("RegistryTask")
  m.LoginTimer = m.top.findNode("LoginTimer")
  m.Token.font.size = 90
  m.Login.font.size = 110

  deviceInfo   = createObject("roDeviceInfo")
  m.model      = deviceInfo.getModel()
  clock        = createObject("roDateTime")
  m.seconds    = clock.asSeconds().toStr()
  m.linked     = false

  m.rokuDeviceID = deviceInfo.GetDeviceUniqueId() + m.model
  m.RegTask.read = m.rokuDeviceID

  m.gen = "http://www.bgb.mx/roku/linking3/generate.php?token=" + m.rokuDeviceID
  m.auth = "http://www.bgb.mx/roku/linking3/generate.php?token=" + m.rokuDeviceID
  m.dis = "http://www.bgb.mx/roku/linking3/generate.php?token=" + m.rokuDeviceID

  m.UriHandler  = createObject("roSGNode","UriHandler")
  m.UriHandler.observeField("linked","onLinked")
  m.UriHandler.observeField("disconnect","onDisconnect")
  m.UriHandler.observeField("response","onNewToken")
  m.LoginTimer.observeField("fire","onCheckAuth")

  m.RegTask.observeField("result","onReadFinished")
  m.Button.observeField("buttonSelected", "onButtonPress")
  m.Button.setFocus(true)
end sub

sub onReadFinished(event as object)
  print "onReadFinished"
  if m.RegTask.result = "none"
    makeRequest({}, m.gen, "GET", 0)
    m.LoginTimer.repeat = true
    m.LoginTimer.control = "start"
  else
    'm.HowTo.translation = [600, 540]
    m.HowTo.text = "You are already linked. To run this demo again, either press 'Unlink Device' or rerun the channel using a different ID string (replace UNIQUE_ID_HERE) to simulate another device."
    m.Token.text = ""
    m.Button.text = "Unlink The Device"
  end if
end sub

sub onLinked(event as object)
  if m.UriHandler.linked
    m.linked = true
    m.HowTo.text = "Your account was successfully linked! To run this demo again, either press 'Unlink Device' or rerun the channel using a different ID string (replace UNIQUE_ID_HERE) to simulate another device."
    m.Token.text = ""
    m.Button.text = "Unlink The Device"
    m.LoginTimer.repeat = false
    m.LoginTimer.control = "stop"
    m.regTask.write = {
      deviceID: m.rokuDeviceID,
      oauth_token: m.urihandler.oauth_token
    }
  else
    print "Not linked yet"
  end if
end sub

sub onDisconnect(event as object)
  if m.UriHandler.disconnect
    m.HowTo.text = "Go to 'http://rokuleow.pythonanywhere.com/activate' on a web browser and enter the following code to connect:"
    m.Button.text = "Generate A New Code"
    m.linked = false
    makeRequest({}, m.gen, "GET", 0)
    m.regTask.write = {
      deviceID: m.rokuDeviceID,
      oauth_token: invalid
    }
  else
    m.HowTo.text = "Error: There was no device to disconnect. Restart the channel."
  end if
end sub

sub onNewToken(event as object)
  print "onNewToken"
  m.Token.text = event.getData()
end sub

sub onCheckAuth(event as object)
  if m.linked
    m.LoginTimer.repeat = false
    m.LoginTimer.control = "stop"
  else
    makeRequest({}, m.auth, "GET", 1)
  end if
end sub

'Generates a new token
sub onButtonPress(event as object)
  print "onButtonPress"
  if m.button.text <> "Unlink The Device"
    makeRequest({}, m.gen, "GET", 0)
    m.LoginTimer.repeat = true
    m.LoginTimer.control = "start"
  else
    makeRequest({}, m.dis, "GET", 2)
    m.LoginTimer.repeat = true
    m.LoginTimer.control = "start"
  end if
end sub

sub makeRequest(headers as object, url as String, method as String, num as Integer)
  print "[makeRequest]"
  context = createObject("roSGNode", "Node")
  params = {
    headers: headers,
    uri: url,
    method: method
  }
  context.addFields({
    parameters: params,
    num: num,
    response: {}
  })
  m.UriHandler.request = { context: context }
end sub


But it doesnt work, i loaded the channel to the roku, and it just appears the word "TOKEN" on the screen, There is a "generate a new code" button but its not clickable, at least no click sound, nothing happens. Maybe i forgot to replace something. But the php script works, at least a new row was created in the table. 

token = YU00FL4635973700X
code = DATZ9U
update_gen = yes
linked = no
0 Kudos
destruk
Binge Watcher

Re: Registration Sdk

It wouldn't be a drop-in replacement.
By changing the token generation from a static serial number+model number source to a randomizer, you'll need to save the token used to the registry in the roku and retrieve it when you need it.
Also your users will need to re-link.
To avoid that issue, you could have the roku channel implement a version check to decide which linking method to use or which method to verify.  Or just keep it in mind for a future channel and leave this one as you had it before.  Your php script is already getting the token variable from the url parameters, so adding a second one like &version would allow you to branch within the same script to do what it needs, or just change the filename for the request.
0 Kudos