Facebook 60天访问令牌

时间:2012-04-23 11:53:22

标签: php facebook api

我目前遇到60天access_token的问题。我抓住了touite access_token通过/我/应用程序,我想发布它们,而不需要每2小时刷新一次网站。

当我以facebook提供的方式解析粉丝的令牌时,它只会引发错误。

登录通过Javascript处理,后台使用PHP SDK处理。

问候, Moritz的

编辑:

{"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);

只需将访问令牌存储在数据库中即可推送到站点。

1 个答案:

答案 0 :(得分: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'];
}