通过PHP检索我的Facebook粉丝专页墙帖现在提供"请求此资源需要访问令牌".

Cyp*_*106 6 php facebook facebook-graph-api

这是一个相当令人愤怒的问题.我们的代码在几个月内完美运行,现在,突然之间,它没有.代码只是使用这个网址抓住我们在粉丝页面上制作的墙贴,并在我们的网站上显示.http://graph.facebook.com/ [我的号码]/feed?limit = 10

现在它不起作用,我花了很多时间在这个问题上筛选搜索结果.不幸的是,我发现的所有内容似乎都指的是Facebook应用程序,而不是粉丝页面.我无法停止看到我需要如何使用我的密钥对其进行身份验证,但是我找不到任何能够显示我的粉丝页的密钥是什么或者是否存在密钥的东西.我不能,因为我的生活,让这个东西工作,我无法弄清楚为什么它只是随机停止工作的第一个地方.

这是我们得到的错误:

{
   "error": {
      "type": "OAuthException",
      "message": "An access token is required to request this resource."
   }
}
Run Code Online (Sandbox Code Playgroud)

编辑:非常感谢Frank Farmer发现这个帖子,问题是需要一个访问令牌,我找不到任何解决方案来获取任何地方.

Nic*_*ick 3

我能够做到这一点的方式是:

  • 创建新的 Facebook 应用程序
  • 使用 Facebook 页面管理员的用户登录 Facebook
  • 打开应用程序的权限请求

    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 、用户位置、用户在线状态、用户关系、用户状态、用户网站、读取好友列表、离线访问、管理页面、广告管理、发布流

您的重定向 URI 必须与您在 Facebook 应用程序的应用程序设置中设置的内容相匹配。

  • 我基本上授予了对该应用程序所有内容的访问权限,您需要确保的主要内容是“manage_pages”

- 之后,您需要复制接受权限请求后转发到的链接的“code=xxxxxx”部分。然后,您可以为用户请求 access_code,一旦获得该代码,您就可以获得该用户作为管理员的 Facebook 页面帖子。

https://graph.facebook.com/oauth/access_token?client_id='the_application_id'&redirect_uri=http://your_redirect_uri/&client_secret='the_code_from_above'
Run Code Online (Sandbox Code Playgroud)
  • 然后它将用访问代码进行响应!

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

下面是一些可以与 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 />";
            }

        }
Run Code Online (Sandbox Code Playgroud)