Hi guys - thought I'd try an experiment in fb app. It certainly seems to have changed a LOT in the last 5y if old online tutorials are anything to go by!
My current code looks like this - with example id's. My current issue is where am I supposed to get the "userid" from to compare against in the token? /confused
I've somewhat bungled my way through with trial & error and code lookups from and so far, and I'm not using the fb API - though if I should be, say so.
This whole project may be irrelevant since now you need SSL for a fb app so I'd have to buy one grrrrr - in the mentime I'm testing via the canvas link directly (using fb link gives blank page, presumably cos no ssl?)
_________________________________
Leozack
My current code looks like this - with example id's. My current issue is where am I supposed to get the "userid" from to compare against in the token? /confused
I've somewhat bungled my way through with trial & error and code lookups from and so far, and I'm not using the fb API - though if I should be, say so.
This whole project may be irrelevant since now you need SSL for a fb app so I'd have to buy one grrrrr - in the mentime I'm testing via the canvas link directly (using fb link gives blank page, presumably cos no ssl?)
PHP:
$appid = 987654321;
$appsecret = "1a2s3d4f5g6h7j8k9l0";
$canvasurl = "[URL unfurl="true"]http://www.webpage.com/app/";[/URL]
$userid = "123456789"; // temp set userid to you - how do I actually get the userID I'm testin the token against?!
// -------------------------------------- GET FB TOKEN
$app_id = $appid;
$canvas_page = $canvasurl;
$auth_url = "[URL unfurl="true"]https://www.facebook.com/dialog/oauth?client_id=".$app_id."&redirect_uri=".urlencode($canvas_page);[/URL]
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
echo "<br>$data";
if (empty($_GET['code'])) { //data["user_id"])) {
//echo "no user id! doh";
echo("<html><head></head><body><script> top.location.href='" . $auth_url . "'</script></body></html>");
} else {
//echo ("Welcome User: " . $data["user_id"]);
}
// -------------------------------------- IF USER DECLINES LOGON
if ($_GET['error_reason']) {
?>
<html><head></head>
<body><h1>Error - <? echo $_GET['error_description']; ?></h1></body>
</html>
<?
exit();
}
// -------------------------------------- IF FB TOKEN - VALIDATE IT
if ($_GET['code']) {
$token = file_get_contents("[URL unfurl="true"]https://graph.facebook.com/oauth/access_token?client_id=".$appid."&redirect_uri=".$canvasurl."&client_secret=".$appsecret."&code=".$_GET[/URL]['code']);
//print_r($token);
$tokentmp = explode("&",$token);
$token = $tokentmp[0];
$tokenexpires = $tokentmp[1];
$tokentmp = explode("=",$token);
$token = $tokentmp[1];
$tokentmp = explode("=",$tokenexpires);
$tokenexpires = $tokentmp[1];
//echo $token." valid for ".(($tokenexpires/60)/60)." hours<br>";
$apptoken = file_get_contents("[URL unfurl="true"]https://graph.facebook.com/oauth/access_token?client_id=".$appid."&client_secret=".$appsecret."&grant_type=client_credentials");[/URL]
$apptokentmp = explode("=",$apptoken);
$apptoken = $apptokentmp[1];
//echo $apptoken;
$inspectedtoken = file_get_contents("[URL unfurl="true"]https://graph.facebook.com/debug_token?input_token=".$token."&access_token=".$apptoken);[/URL]
//$inspectedtoken = file_get_contents("graph.facebook.com/me?access_token=".$token);
//print_r($inspectedtoken);
$json = json_decode($inspectedtoken, true);
//print_r($json[data]);
if ($json[data][user_id] == $userid) { echo "User confirmed<br>"; }
if ($json[data][app_id] == $appid) { echo "App confirmed<br>"; }
//exit();
}
_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);