Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving my Facebook fan page wall posts via PHP now gives "An access token is required to request this resource."

This is a rather infuriating problem. We had code that was working perfectly fine for months and now, all of the sudden, it doesn't. The code just used this url to grab the wall posts we were making on our fan page and showed them on our site. http://graph.facebook.com/[our number]/feed?limit=10

Now it doesn't work and I've spent a ridiculous number of hours sifting through search results on this issue. Unfortunately, everything I find seems to be referring to Facebook apps and not fan pages. I can't stop seeing how I need to authenticate it using my secret key, but I can't find anything that shows me what my fan page's secret key is or if one even exists. I can't, for the life of me, get this thing working and I can't figure out why it just randomly stopped working in the first place.

Here's the error we get:

{
   "error": {
      "type": "OAuthException",
      "message": "An access token is required to request this resource."
   }
}

EDIT: So thanks much to Frank Farmer for finding that post, the problem is needing an access token, which I can't find ANY solution to getting ANYWHERE.

like image 403
Cyprus106 Avatar asked Jun 06 '11 18:06

Cyprus106


2 Answers

The way that I was able to do this was by:

  • Creating a new facebook application
  • Logged into Facebook with the user that is an admin for the Facebook Page
  • Opened up a request for permissions for the application

    https://www.facebook.com/dialog/oauth?client_id='the_application_id'&redirect_uri=http://your_redirect_uri/&scope=email,read_stream,user_birthday,user_about_me,user_likes,read_stream,user_education_history,user_work_history,user_groups,user_hometown,user_religion_politics,user_location,user_online_presence,user_relationships,user_status,user_website,read_friendlists,offline_access,manage_pages,ads_management,publish_stream

Your redirect URI MUST match what you have set into the application settings in your facebook application.

  • I basically granted access to everything for this application, the main thing you need to make sure of is 'manage_pages'

-After that you will need to copy the 'code=xxxxxx' portion of the link that you are forwarded onto after accepting the permissions request. You can THEN request an access_code for the user, once you have that, you can get the Facebook page posts that the user is an admin to.

https://graph.facebook.com/oauth/access_token?client_id='the_application_id'&redirect_uri=http://your_redirect_uri/&client_secret='the_code_from_above'
  • It will then respond with an access code!

https://graph.facebook.com/feed?access_token='your_access_token'

Below is some sample code you can use with the PHP facebook SDK:

define('APP_ID', 'your_app_id');
        define('APP_API_KEY', 'your_app_api_key');
        define('APP_SECRET', 'your_app_secret');

        $fb = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET,
            'cookie' => false
        ));

        $fb_user_id   = 'user_id_of_person_that_has_page_admin_rights';
        $access_token = urlencode('your_access_token_you_received');

        try {

            $user = $fb->api('/'.$fb_user_id,'GET',array('access_token'=>$access_token));
            $accounts = $fb->api('/'.$fb_user_id.'/accounts','GET',array('access_token'=>$access_token));

        } catch (FacebookApiException $e) {

            echo $e->getMessage();

        }

        echo "<strong>User Details:</strong><br />";
        foreach($user as $key => $value){
            echo ucwords(str_replace("_"," ",$key)).": ".$value."<br />";
            $fb->api('/feed','POST',array('access_token'=>$access_token,'id'=>$user_id,'message'=>'Add a post to the user's wall'));
        }

        echo "<br /><strong>Accounts Details:</strong><br />";
        foreach($accounts['data'] as $account){
            foreach($account as $key => $value){
                echo ucwords(str_replace("_"," ",$key)).": ".$value."<br />";
            }

            try {

                $posts = $fb->api('/'.$account['id'].'/posts','GET',array('access_token'=>$account['access_token']));

            } catch (FacebookApiException $e) {

                echo $e->getMessage();

            }

            echo "<br /><strong>-- Posts for this account:</strong><br />";

            foreach($posts['data'] as $post){
                foreach($post as $key => $value){
                    echo ucwords(str_replace("_"," ",$key)).": ".$value."<br />";
                }
                echo "<br />";
            }

        }
like image 195
Nick Avatar answered Nov 11 '22 23:11

Nick


Yeah, it requires a token now. They announced this via their blog.

http://developers.facebook.com/blog/post/509/

Breaking change: Graph API PROFILE_ID/feed and PROFILE_ID/posts requires access_token

The Graph API PROFILE_ID/feed/ for a Page, Application, User or Group and PROFILE_ID/posts for a Page or User will now require a vaild access_token to access the wall or posts of the corresponding object (where previously no access_token was required).

Looks like they only gave a week notice.

You should subscribe to the RSS feed for their dev blog. They pull stuff like this all the time, although usually they give a little more notice.

like image 2
Frank Farmer Avatar answered Nov 11 '22 23:11

Frank Farmer