2012-02-20 148 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