I keep finding that after a few days I'm no longer able to post to Facebook from what I'm assuming is due to the access token expiring. This isn't very practical as you don't know there's a issue until you add a node, and I'm also not always about to re-login

I have found that there is a way to apparently get a long lived 60 day token using the following $fb->setExtendedAccessToken(); so I've added it like below but guess I won't know for a while as to weather it has worked or now.

function fb_autopost_facebook_login() {
  // Show error messages from Facebook.
  if (isset($_GET['error'])) {
    drupal_set_message(t('An error happened while connecting with Facebook. Reason: %reason. Description: %description', array('%reason' => $_GET['error_reason'], '%description' => $_GET['error_description'])), 'error');
  }
  else {
    try {
      $fb = new FBAutopost();
      if ($fb_user = $fb->getUser()) {
      	$fb->setExtendedAccessToken();
        $token = $fb->getAccessToken();
        variable_set('fb_autopost_token', $token);
        $user_profile = $fb->api('/me?fields=id');
        variable_set('fb_autopost_account_id', $user_profile['id']);
      }
    }
    catch (FacebookApiException $e) {
      // Do nothing with the exception.
      drupal_set_message($e->getMessage(), 'error');
    }
  }
  drupal_goto('admin/config/services/fbautopost');
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

e0ipso’s picture

It's been a long time since I dealt with this, but I see this when debugging my access token (which I got by typing drush vget --exact fb_autopost_token)

Access token

e0ipso’s picture

Category: Feature request » Bug report
Status: Active » Postponed (maintainer needs more info)
WebWalker3D’s picture

I have run into this issue as well. According to Facebook:

getLongLivedSession(string $appId = NULL, string $appSecret = NULL)
Returns a new Facebook\FacebookSession resulting from extending a short-lived access token. This method will make a network request. If you know you already have a long-lived session, you do not need to call this. The only time you get a short-lived session as of March 2014 is from the Facebook SDK for JavaScript. If this session is not short-lived, this method will return $this. A long-lived session is on the order of months. A short-lived session is on the order of hours. You can figure out whether a session is short-lived or long-lived by checking the expiration date in the session info, but it's not a precise thing.

source: https://developers.facebook.com/docs/php/FacebookSession/5.0.0

If this is the case, why are long lived access tokens not being issued to users of Facebook Autopost?

Access tokens were also explained as automatically refreshing when they are used. e.g. If I have a long-lived session (which is at least 60 days) and I use it tomorrow, that 60 days restarts. In other words, my access token should never expire.

Native mobile apps using Facebook's SDKs will get long-lived access tokens, good for about 60 days. These tokens will be refreshed once per day when the person using your app makes a request to Facebook's servers. If no requests are made, the token will expire after about 60 days and the person will have to go through the login flow again to get a new token.

source: https://developers.facebook.com/docs/facebook-login/access-tokens/expira...

Some insight would be helpful. I believe the related issue is also questioning this.

WebWalker3D’s picture

Component: Miscellaneous » Code
Status: Postponed (maintainer needs more info) » Active
arsn’s picture

Hello,

@marcus178 I had the same issue and thought I would solve it the same way you suggested it until I applied the proper permissions under admin/config/services/fbpermissions and connected once more.

Then thanks to @e0ipso test #2138415#comment-8300507 I could check that my Token is now unlimited and it seems to work.

The permissions I've set are the one I had to request to Facebook and being manage_pages and publish_pages, under the page tab (once you have selected the role to give the permission to). Indeed we read under the Manage pages checkbox of the Facebook permission configuration page:

Enables your application to retrieve access_tokens for Pages and Applications that the user administrates. The access tokens can be queried by calling /{user_id}/accounts via the Graph API. See here for generating long-lived Page access tokens that do not expire after 60 days.

Hope this help. If you had the same issue and solve it this way, we may close this issue as the module works as designed when properly configured.