Php Facebook API Page access tokens

Eric picture Eric · Aug 3, 2013 · Viewed 9.3k times · Source

I'm trying to build a plugin for question2answer (knowledge not necessary) where when a new question is posted, it posts that question as an activity on the facebook page. Achieving this is not a problem. what I run into, is persistence.

From what I understand, and please correct me if I'm wrong, is that you can only get access token directly for a page through the account of an admin user. This creates a problem for posting when the administrator of the page whose access token we've stored expires from logging out. Well, we can't expect them to stay logged in forever, right?

Is there another way to go about this? Like maybe getting the users to post on that page instead?

Here is the code I am currently using (this is not final, no error handling). Anything that says qa_opt is just something stored in the question2answer database

$facebook2 = new Facebook(array(
                                                    'appId' => qa_opt('facebook_app_id'),
                                                    'secret' => qa_opt('facebook_secret')

                        ));

                        $facebook2->setAccessToken(qa_opt('facebook_page_access_code'));

                        // Try to extend token
                        $access_token = $facebook2->getExtendedAccessToken();



                        // As is, with the extended token, we currently post as the user, not the page. Let's fix that
                        $accounts = $facebook->api('/me/accounts?access_token='.$access_token);
                        foreach ( $accounts as $account )
                        {
                            if ( $account['id'] == qa_opt('facebook_page_id') )
                            {
                                $page_access_token = $account['access_token'];
                                break; // Stop processing foreach
                            }

                        }

                        $fbPageArgs = array('access_token' => $page_access_token,
                                    'message' => 'A new question has been created!',
                                    'link' => qa_q_path($params['postid'], $params['title'], true),
                                    'name' => $params['title'],
                                    'description' => $params['text'] 
                        );

                        $facebook2->api("/".qa_opt('facebook_page_id')."/feed?fields=access_token","post",$fbPageArgs);

Answer

Sahil Mittal picture Sahil Mittal · Aug 3, 2013

You can have a never expiring token for your fan page, that will solve your problem i guess.

Follow the simple steps:

  1. Get the admin's(i.e. your's) extended token (2 months validity). Go though the link to get the long-lived token.

    Extending tokens

  2. Get the never expiring access token for any page using this token-

    $facebook->api("/PAGE_ID?fields=access_token");
    

(You can use Facebook's Debug Tool to check the validity of the token).