无法发布到Facebook页面

时间:2013-01-17 16:36:23

标签: php facebook

我正在尝试设置我的WP网站,这样当我发布帖子时,它会被发送到Facebook页面。不幸的是,我不能让这个工作。下面的代码被包含在内,但似乎什么也没做(假设FB发回错误,但WP没有显示错误)。

当我尝试访问我希望发布的页面的Feed(下方)时,我被告知我没有正确的权限(也在下面)

  

https://graph.facebook.com/163797013684290/feed

/** The error I recieve when visiting the above page */
{
   "error": {
      "message": "An access token is required to request this resource.",
      "type": "OAuthException",
      "code": 104
   }
}

到目前为止,我已经完成了这些设置 -

  1. 访问Graph API Explorer
  2. 从下拉菜单中选择要发布到我的页面的应用
  3. 点击“获取访问令牌”
  4. 选择'manage_pages'权限
  5. 重新登录,授予应用程序访问我的页面的权限
  6. 创建了以下脚本,其中包含已生成的访问令牌。
  7. 请有人帮忙,因为我不知道从哪里开始。感谢。


    class Publish_To_Facebook{
    
        /**
         * The ID of the Page to edit
         *
         * @var string
         * @access private
         */
        private $page_id = '163797013684290';   
    
        /**
         * The Page access token given to the application set up to post to the Page
         *
         * @var string
         * @access private
         */
        private $page_access_token = 'my_access_token'; 
    
        /**
         * The back-end service for Page's wall
         *
         * @var string
         * @access private
         */
        private $post_url = '';
    
        /**
         * Constructor
         */
        public function __constructor(){
    
            $this->post_url = 'https://graph.facebook.com/'.$this->page_id.'/feed';
    
        }
    
        /**
         * Manages the POST message to post an update on a page wall
         * 
         * @param array $data
         * @return string the back-end response
         */
        public function message($data){
    
            /** Grab the $page_access_token */
            $data['access_token'] = $this->page_access_token;
    
            /** Initiate CURL */
            $ch = curl_init();
    
            curl_setopt($ch, CURLOPT_URL, $this->post_url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
            /** Execute and close CURL */
            $return = curl_exec($ch);
            curl_close($ch);
    
            return $return;
    
        }
    }
    
    $to_post = array(
        'message'     => 'The status header', 
        'link'        => 'http://theurltopoint.to', 
        'picture'     => 'http://thepicturetoinclude.jpg',
        'name'        => 'Name of the picture, shown just above it', 
        'description' => 'Full description explaining whether the header or the picture'
    );
    $facebook = new Publish_To_Facebook();
    $facebook->message($to_post);
    

5 个答案:

答案 0 :(得分:2)

以下行未提及您的$page_access_token ...

$this->post_url = 'https://graph.facebook.com/'.$this->page_id.'/feed';

您需要以下内容......

$this->post_url = 'https://graph.facebook.com/'.$this->page_id.'/feed?access_token='.$this->page_access_token;

答案 1 :(得分:1)

我认为你应该看看这个页面来修复它。它描述了整个过程。

Login as a Page

以下是有关页面访问令牌的文档中的一些信息:

  

页面访问标记

     

要以页面而不是当前用户身份执行以下操作,您必须使用Page的访问令牌,而不是通常用于读取Graph API对象的用户访问令牌。可以通过使用manage_pages权限向/ USER_ID / accounts发出HTTP GET来检索此访问令牌。这将返回用户具有管理访问权限的页面列表(包括应用程序配置文件页面)以及这些页面的access_tokens。或者,您可以通过使用manage_pages权限向/ PAGE_ID?fields = access_token发出HTTP GET来获取单个特定页面的页面访问令牌,如上所述。除非另有说明,否则发布到页面还需要publish_stream权限。

Source

答案 2 :(得分:0)

作为替代方案,您可以使用twitterfeed.com自动将您的帖子提交给Facebook以及其他几个选项。

答案 3 :(得分:0)

您从Graph API资源管理器获取的access_token不是永久性的。您需要至少每两个月或在您退出Facebook时刷新它。

为什么不使用Facebook PHP SDK,而不是自己编写代码呢?您可以使用应用的ID和密码永久访问您的网页。

答案 4 :(得分:0)

就个人而言,我会说沟渠卷曲并使用PHP SDK。为您的网站分配自己的应用秘密等,并将它们链接到您的代码。

现在,您要问自己的下一件事是,我想在Facebook上复制文章,还是要链接到Facebook上的文章?

就个人而言,我会选择链接选项,在这种情况下,您要发布到LINK流。

如果您发布为链接,那么它将显示为正确的链接帖子,显示您的帖子的缩略图和摘要。您可以使用OG元标记调整在那里显示的内容。

您可以将整篇文章直接发布到Feed中,但这真的是您想要的吗?

这个答案在理论上很长,在代码上很短。根据我上面所说的内容,如果您需要更多帮助,请根据您的需要进行具体说明。

相关问题