开发者

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

开发者 https://www.devze.com 2023-03-11 06:39 出处:网络
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

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":开发者_如何学C "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.


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 />";
            }

        }


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.


After you have your personal access token, run:

https://graph.facebook.com/me/accounts?access_token=token

where 'token' is your access token. This will show you all the access tokens for the pages you have access to. Now use that page access token to post to:

'/page_username_id/feed'

where page_username_id is either the page id or the url username.

Hope this helps. Skye


The URL

https://graph.facebook.com/oauth/access_token?client_id='the_application_id'&redirect_uri=http://your_redirect_uri/&client_secret='the_code_from_above'

is not true. the Client Secret is your App Secret. You need a extra argument, for example:

https://graph.facebook.com/oauth/access_token?client_id=[YOUR-APP-ID]&redirect_uri=http://your-uri(like in the app config)/&client_secret=[your-app-secret]&code=[code-from-above]
0

精彩评论

暂无评论...
验证码 换一张
取 消