2016-04-24 160 views
-1

我有CodeIigniter 3項目,我有一個博客帖子功能,我想添加一個自動發佈到Facebook頁面。我已經用我的CodeIgniter 3項目配置了HyBridAuth。此外,我已經驗證了我的Facebook並使用訪問令牌存儲了Facebook會話的數據。作爲頁面管理員在Facebook頁面上發佈帖子

現在,我試圖張貼到我的臉書頁面。我已經創建了以下方法。

public function facebook_post() { 
    if ($this->ion_auth->logged_in()) { 
     $this->data['userInformation'] = $this->ion_auth->user()->row(); 
    } 
    $this->data['userid'] = $this->data['userInformation']->id; 
    $this->data["facebook_status"] = $this->admin_model->getStoredHybridSession($this->data['userid'], "Facebook"); 

    if(!empty($this->data['facebook_status'][0])) { 
     if($this->data["facebook_status"] != "0") { 
      $this->data["facebook_profile"] = $this->get_profile("Facebook"); 
      $access_token = explode('&', $this->data['facebook_profile']->coverInfoURL); 
      $a_t = explode('=', $access_token[1]); 

      $params = array(
       "access_token" => $a_t[1], 
       "message" => "Here is a blog post about auto posting on Facebook using PHP", 
       "link" => "http://facebook.com", 
       "picture" => "https://i.telegraph.co.uk/multimedia/archive/03474/Facebook_3474124b.jpg", 
       "name" => "Auto post functionality test", 
       "caption" => "https://facebook.com", 
       "description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation." 
      ); 
      $facebook = $this->hybridauthlib->authenticate("Facebook"); 
      $ret = $facebook->api()->api('/1388840631445245/feed', 'POST', $params); 
      owndebugger('Successfully posted to Facebook'); 
     } 
    } else { 
     $this->data["facebook_profile"] = 0; 
    } 
} 

此代碼正在工作。但是,我的帖子張貼到頁面作爲訪客帖子。

我做錯了什麼?

+0

沒有檢查你的代碼,你很可能使用用戶令牌而不是頁面令牌。 – luschn

+0

@luschn - 頁面令牌? :(這對我來說似乎很新詞,我怎麼弄的? –

+1

http://www.devils-heaven.com/facebook-access-tokens/ – luschn

回答

0

我已成功完成。 :)

要在頁面上發佈以下信息需要考慮。

1)適用範圍:publish_pages, publish_actions, manage_pages

2)你要存儲長住access_token在一個頁面後定期。

3)然後,你必須在您的文章的方法來與你現有的存儲會話重新登錄如下:

$this->data["facebook_profile"] = $this->get_user_pages("Facebook"); 
    $params = array(
    "access_token" => $this->data['facebook_profile'][0]['access_token'], 
    "message" => $this->input->post('posttitle'), 
    "link" => base_url() . "news/" . $this->results, 
    "picture" => base_url() . "uploads/posts/".(isset($profilephoto['file_name']) ? $profilephoto['file_name'] : $pp), 
    "name" => "Stack Overflow", 
    "caption" => "http://stackoverflow.com", 
    "description" => $this->input->post('postcontent') 
    ); 
// post on a single page of my pages 
    $facebook = $this->hybridauthlib->authenticate("Facebook"); 
    $facebook->api()->api("/221320344636/feed", 'POST', $params); 

4)$this->get_user_pages("Facebook")。這裏get_user_pages檢索您的所有頁面access_token,然後您可以選擇access_token您的某個頁面在該頁面上發佈。

我想特別感謝@luschn的幫助。 :)

相關問題