Today I tried almost everything with the Facebook API. I'm using the PHP SDK. It worked out to log in as a user, as a page and as an app.
I have made the most simple login system as possible now:
Login.php
$app_id = "xx";
$app_secret = "xx";
$my_url = "continue.php";
session_start();
// TOESTEMMING VRAGEN
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'] . "&scope=manage_pages";
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
At continue.php I have this code:
session_start();
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
$graph_url = "https://graph.facebook.com/me/accounts?access_token="
. $params['access_token'];
$accounts = json_decode(file_get_contents($graph_url));
print_r($accounts);
} else {
echo("The state does not match. You may be a victim of CSRF.");
}
I'm getting the login, I can give access, but when I continue I get this message:
stdClass Object ( [error] => stdClass Object ( [message] => An active access token must be used to query information about the current user. [type] => OAuthException [code] => 2500 ) )
What I try to achieve is: On an admin part of my website I want to create a dialog where administrators can post updates to a facebook-page as a page-account. I also tried to login as an app, but I don't know if that's the right solution.
I can't find a tutorial on the web about which approach is needed for which solution. The advantage of logging in as an app, is that administrators don't need to have a facebook account. But can I (as an app) post an update to a facebook-page?
Sorry, I'm a bit confused. Thanks.