2012-04-23 190 views
0

我目前有60天access_token的问题。我抓住粉丝团的access_token thru/me/applications,并且想发布它们,而不需要每2小时刷新一次该网站。Facebook 60天访问令牌

当我在Facebook提供它的方式解析fansite的标记只是抛出一个错误。

登录通过Javascript进行处理,并在PHP SDK的后台进行处理。

问候, 莫里茨

编辑:

{"error_code":1,"error_msg":"An unknown error occurred"}是错误。

代码是这样的:

$attachment = array('message' => $post['title'], 
         'link' => $unique, 
         'actions' => '{"name": "Fanseiten Admin?", "link": "http://google.com"}', 
         'access_token' => $page['access_token']); 
$result = $facebook->api('/'.$page['pageid'].'/feed', 'post', $attachment); 

简单地推动与存储在数据库中的访问令牌的站点。

+4

什么是代码?什么是错误?你在说什么? :) – 2012-04-23 11:57:49

+0

嘿, 我更新了正文。 – Moritz 2012-04-23 13:35:40

+0

您是否尝试将页面访问令牌作为查询字符串参数而不是表单发布参数? – DMCS 2012-04-23 17:24:41

回答

3

我找到了解决办法:用以下扩展base_facebook.php类并调用

$facebook->setAccessToken($facebook->getExtendedAccessToken()); 

的对的access_token也应用现在扩展到60天。

public function getExtendedAccessToken(){ 

    try { 
     // need to circumvent json_decode by calling _oauthRequest 
     // directly, since response isn't JSON format. 
     $access_token_response = 
      $this->_oauthRequest(
       $this->getUrl('graph', '/oauth/access_token'), array(
        'client_id' => $this->getAppId(), 
        'client_secret' => $this->getAppSecret(), 
        'grant_type'=>'fb_exchange_token', 
        'fb_exchange_token'=>$this->getAccessToken() 
       ) 
      ); 
    } catch (FacebookApiException $e) { 
     // most likely that user very recently revoked authorization. 
     // In any event, we don't have an access token, so say so. 
     return false; 
    } 

    if (empty($access_token_response)) { 
     return false; 
    } 

    $response_params = array(); 
    parse_str($access_token_response, $response_params); 
    if (!isset($response_params['access_token'])) { 
     return false; 
    } 

    return $response_params['access_token']; 
}