2012-02-20 144 views
0

我有一個劇本,我想通過一個cron作業運行:Facebook的API離線訪問

<? 

require_once 'fb_access.php'; 

$user = $facebook->getUser(); 

    if ($user) { 
     try { 
     $page_id = '********'; 
     $page_info = $facebook->api("/$page_id?fields=access_token"); 
     if(!empty($page_info['access_token'])) { 
      $args = array(
       'access_token' => $page_info['access_token'], 
       'message'  => 'This is the message', 
       'link'   => 'http://thisisthelink', 
       'caption'  => 'This is the caption', 
       'description' => 'This is the description', 
      ); 
      $post_id = $facebook->api("/$page_id/feed","post",$args); 
     } 
     } catch (FacebookApiException $e) { 
     error_log($e); 
     $user = null; 
     } 
    } 
    ?> 

然而,當我嘗試通過cron作業來運行它,它不工作。我發現這與offline_access令牌有關。但從我讀到的,用戶必須手動登錄才能獲取令牌。因爲我想通過cron工作來運行它,所以這是不可行的。有任何想法嗎?

更新:

我試過了;這是完全錯誤的嗎?

require_once 'fb_access.php'; 

$user = $facebook->getUser(); 
$token = $facebook->getAccessToken(); 

if ($user) { 
    try { 
     $page_id = '**********'; 
     $args = array(
      'access_token' => $token, 
      'message'  => 'This is the message', 
      'link'   => 'http://thisisthelink', 
      'caption'  => 'This is the caption', 
      'description' => 'This is the description', 
     ); 
    $post_id = $facebook->api("/$page_id/feed","post",$args); 
    } 
    catch (FacebookApiException $e) { 
     error_log($e); 
     $user = null; 
    } 
} 

更新#2:

<?php 

require_once 'fb_access.php'; 

$user = $facebook->getUser(); 
$token = '*******LONG_ACCESS_TOKEN*******'; 

$page_id = '**********'; 
$args = array(
    'access_token' => $token, 
    'message'  => 'This is the message', 
    'link'   => 'http://www.thisisthelink.com', 
    'caption'  => 'This is the caption', 
    'description' => 'This is the description', 
); 

$post_id = $facebook->api("/$page_id/feed","post",$args); 

?> 

使用此我得到:致命錯誤:Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in /base_facebook.php on line 1106

+0

等等......現在,即使我手動啓動腳本,這也不起作用。幫幫我!這令人沮喪。 – Andrew 2012-02-20 21:19:17

+0

你的updatE:是的,這是錯誤的。您需要先擁有訪問令牌(登錄用戶)。看看這個頁面,https://developers.facebook。com/tools/explorer /'和「獲取訪問令牌」,並具有manage_pages權限。獲得後,輸入'/ accounts /'到地址欄(在頁面中)並按提交。你應該看到你的頁面和訪問令牌。將'$ facebook-> getAccessToken()'從您的代碼更改爲該訪問令牌,停止嘗試檢查'$ user'並重試。 – 2012-02-20 23:05:52

+0

嗯...試圖做到這一點,但我發現: '{ 「錯誤」:{ 「消息」: 「(#210)除必須的頁面。」, 「類型」:「OAuthException 「, 」code「:210 } }' – Andrew 2012-02-21 05:38:33

回答

3
  • 首先,你不需要offline_access,如果你需要publish_stream許可。
  • 如果您想要登錄/管理/共享您的頁面,您需要manage_pages權限。
  • 您需要用戶訪問令牌才能獲取頁面訪問令牌。你需要在你得到一個訪問令牌認證一次手動

header("Location: $facebook->getLoginUrl(array("scope" => "publish_stream,manage_pages"))); 
    • ,將它保存到永久位置(文件,例如)
    • 需要時,從您的文件中獲取訪問令牌並使用它(只需添加訪問權限_token到你的論點)

下面的代碼工作對我說:

<?php 
include(dirname(__FILE__)."/fb/facebook.php"); 
define('PAGE_ID', '123456498798'); 
$facebook = new Facebook(array(
    'appId' => '178645249555182', 
    'secret' => 'csdf64sd65f4sd6f54f1c', 
)); 

$a = array(
    'access_token' => 'werf564s6d1cr98f965d6gf49w8sd49f87w9ed5c16d5f49s8f74w9e8rf74', 
    'message'  => 'This is the message', 
    'caption'  => 'This is the caption', 
    'description' => 'This is the description' 
); 
print_r($facebook->api('/'.PAGE_ID.'/feed', 'post', $a)); 
+0

嗯...我的應用程序具有這些權限。我如何**保存**訪問令牌?我不是在上面的代碼中引用它嗎?例如, – Andrew 2012-02-20 21:01:49

+0

「How」=>手動。當用戶登錄時,您將獲得帶有$ facebook-> getAccessToken()的訪問令牌 – 2012-02-20 21:24:53

+0

用戶永遠不會登錄。我希望此腳本從預定義的帖子數據庫中提取並通過cron作業發佈到Facebook頁面。 見上文。 – Andrew 2012-02-20 21:34:49

2

如果它是一個cron作業,有沒有人有做的授權使用。你不能做你想做的。你需要尋找替代品。

另一種方法就像從您請求擴展令牌的其中一個頁面管理員處獲取有效身份驗證令牌(offline_access正在迅速棄用,請參閱https://developers.facebook.com/docs/offline-access-deprecation/)。

這60天的令牌需要放入cron作業可以訪問的某個配置中。

您需要每隔幾個月手動更新60天令牌。

您可以從https://developers.facebook.com/tools/explorer獲得有效的訪問令牌。

+0

您可以從https://developers.facebook.com/tools/explorer獲得有效的訪問令牌,這對我來說是最重要的部分! – adontz 2014-05-13 20:56:17