You are here

function fb_instant_articles_api_facebook_login in Facebook Instant Articles 7

Same name and namespace in other branches
  1. 7.2 modules/fb_instant_articles_api/fb_instant_articles_api.module \fb_instant_articles_api_facebook_login()

Callback to handle Facebook Login for this module.

1 string reference to 'fb_instant_articles_api_facebook_login'
fb_instant_articles_api_menu in modules/fb_instant_articles_api/fb_instant_articles_api.module
Implements hook_menu().

File

modules/fb_instant_articles_api/fb_instant_articles_api.module, line 48
Hook implementations for Facebook Instant Articles API module.

Code

function fb_instant_articles_api_facebook_login() {
  $fb = new Facebook\Facebook([
    'app_id' => variable_get('fb_instant_articles_api_app_id', ''),
    'app_secret' => variable_get('fb_instant_articles_api_app_secret', ''),
    'default_graph_version' => 'v2.5',
  ]);
  $helper = $fb
    ->getRedirectLoginHelper();

  // Grab the user access token based on callback response and report back an
  // error if we weren't able to get one.
  try {
    $access_token = $helper
      ->getAccessToken();
  } catch (Facebook\Exceptions\FacebookResponseException $e) {
    $error_msg = 'We received the following error while attempting to authenticate your Facebook account: ' . $e
      ->getMessage();
    drupal_set_message($error_msg, 'error');
    drupal_goto('admin/config/services/fb-instant-articles/api');
    exit;
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
    $error_msg = 'We received the following error while attempting to authenticate your Facebook account: ' . $e
      ->getMessage();
    drupal_set_message($error_msg, 'error');
    drupal_goto('admin/config/services/fb-instant-articles/api');
    exit;
  }
  if ($access_token == NULL) {
    $error_msg = 'We failed to authenticate your Facebook account with this module. Please try again.';
    drupal_set_message($error_msg, 'error');
    drupal_goto('admin/config/services/fb-instant-articles/api');
    exit;
  }

  // Confirm that the person granted the necessary permissions before proceeding
  $permissions = $fb
    ->get('/me/permissions', $access_token)
    ->getGraphEdge();

  //  $rejected_permissions = array();
  $rejected_permissions = '';
  foreach ($permissions as $permission) {
    if ($permission['status'] != 'granted') {
      $rejected_permissions = $rejected_permissions . ' ' . $permission['permission'];
    }
  }
  if ($rejected_permissions != '') {
    $error_msg = 'You did not grant the following required permissions in the Facebook authentication process: ' . $rejected_permissions . '. Please try again.';
    drupal_set_message($error_msg, 'error');
    drupal_goto('admin/config/services/fb-instant-articles/api');
    exit;
  }

  // Store this user access token to the database
  variable_set('fb_instant_articles_api_access_token', $access_token
    ->getValue());
  drupal_goto('admin/config/services/fb-instant-articles/api');
}