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