<?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;
}
?>
' ********** 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
We’re upgrading Roku Community to bring you a faster, more mobile-friendly experience. You may notice limited functionality or read-only access during this time. You will not be able to log in or post new comments or kudos during this time. Read more here.
Planned Downtime:
Community will be unavailable for up to 24–48 hours during the upgrade window during the week of May 12 and you may notice reduced functionality.
In the meantime, for additional assistance, visit our Support Site.
Thanks for your patience — we’re excited to share what’s next!