1

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.

p3sn
  • 1,052
  • 1
  • 9
  • 17
  • 1
    Where is your code for posting to a page? – phwd Oct 16 '12 at 19:24
  • I didn't even came that far, because everytime there was something with authentication... Like an active access token in my message above. – p3sn Oct 16 '12 at 21:07
  • 1
    An access token is the way of Facebook to identify your request. Did you get one ? Did you assign it to $params['access_token'] ? If both answers are positive, show us the code, because based on the error message, there is obviously a problem with the access token. – Alon Adler Oct 16 '12 at 21:24
  • as in @Alon_A's comment, plus check out this [answer](http://facebook.stackoverflow.com/questions/3627684/facebook-graph-api-posting-to-fan-page-as-an-admin/7649048#7649048). – ifaour Oct 16 '12 at 21:38
  • Thanks for your replies. These are all methods where I need a facebook user. I want to create a solution where the admins of the website doesn't has to have a facebook account, but can log in directly as a facebook page or app. So he can post messages to the page wall as a page or app. – p3sn Oct 19 '12 at 11:12

2 Answers2

0

Your code is incomplete. I suggest you re-read http://developers.facebook.com/docs/howtos/login/server-side-login/

Before you can get the Page Access Token, you need the User Access Token

   if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $app_secret . "&code=" . $code;

     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);

     $_SESSION['access_token'] = $params['access_token'];

     $graph_url = "https://graph.facebook.com/me?access_token=" 
       . $params['access_token'];

     $user = json_decode(file_get_contents($graph_url));
     echo("Hello " . $user->name);
   }
   else {
     echo("The state does not match. You may be a victim of CSRF.");
   }
phwd
  • 19,975
  • 5
  • 50
  • 78
  • Thanks for your reply, indeed it's incomplete. But that's because I didn't get the result I wanted. Is it possible to login as a page or as an app without having facebook? At this moment I am creating a website. The website also has a facebook page. But not all admins do have facebook. Is this possible to login (via PHP) as page or app at all, and then post something on the page's wall? – p3sn Oct 19 '12 at 08:06
  • If you use a page token via a user token from a valid user on Facebook who is an admin of your page, it can work. See this http://philippeharewood.com/facebook/getting-your-facebook-page-on-your-website-with-access-tokens/ which shows how to display page posts without having to constantly authenticate. – phwd Oct 20 '12 at 05:03
  • Maybe I can use this process... But the example is getting information from Facebook. I want to login at my website as a page and post to the page's wall like a facebook as a page, not as an individual. I don't know if it's possible at all. – p3sn Oct 21 '12 at 17:39
  • @p3sn you cannot login to Facebook as a page (except under the case of a business account) so yes that's not possible. You **need** a Facebook user at some point during the process. – phwd Oct 21 '12 at 18:08
  • What's the difference then between a business-account-page and a page I created from my personal account? I thought I could create page's (as an individual) for a business too. #confused – p3sn Oct 22 '12 at 10:32
  • Business accounts are for advertising. They have limited uses. All that's important for this question is that since you cannot login as a page on Facebook.com without logging in as a user, you cannot do otherwise in an application. – phwd Oct 22 '12 at 11:32
0

Basic example to post on a Page you own (posting as the Page):

Make sure user/page_owner has auth'd the app and is logged in (usual FB API methods). User must have manage_pages (admin) permissions for the page.

$facebook = new Facebook(... );
$user = $facebook->getUser();

Get the page access token:

$page_id = 123456;
$page_info = $facebook->api('/'.$page_id.'?fields=access_token');

Use the page access token to post to your Page:

if (!empty($page_info['access_token'])) {
  $args = array(
    'access_token' => $page_info['access_token'],
    'message' => 'Testing SDK !',
  );
  $post_id = $facebook->api('/'.$page_id.'/feed', 'post', $args);
  error_log('successfully posted post id '.$post_id);
} catch (FacebookApiException $e) {
  error_log('caught exception: '. $e);
}

If you still get an access token error, log the user access token and test/paste the user access token in the Debugger and verify manage_pages perms is enabled.

See also this helpful blog post on Page permissions.

Donn Lee
  • 2,962
  • 1
  • 24
  • 16
  • Thanks for your reply. I already replied phwd too. I'm searching for a solution to login as app (wich I tried once succesfull too). But is it possible to post on the page's wall as an app too? I want to login as app, because not all administrators of the website do have facebook. Thanks. – p3sn Oct 19 '12 at 08:08
  • You cannot post on a Page as an App. – Donn Lee Oct 19 '12 at 21:25
  • Hmm okay, that clears things up. So I can forget logging in as an app. Thanks! – p3sn Oct 21 '12 at 17:41