Facebook 60 day access token-Collection of common programming errors

I am currently having issues with the 60 day access_token. I grab the fansite access_token thru /me/applications and I want to post on them, without the need for refreshing the site every 2 hours.

When I’m parsing the token for the fansite in the way facebook provides it just throws an error.

The login is handled thru Javascript and in the background with the PHP SDK.

Greetings, Moritz

Edit:

{"error_code":1,"error_msg":"An unknown error occurred"} is the error.

Code is this:

$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);

Simply pushes to a site with the access token stored in a database.

  1. I found the solution: extend the base_facebook.php class with following and call

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

    The access_token for apps also now extends to 60 days.

    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'];
    }
    

Originally posted 2013-11-09 23:13:25.