You are here

public function FacebookAuthManager::getAccessToken in Social Auth Facebook 8

Gets the user's access token from Facebook.

This method can only be called from route social_auth_facebook.return_from_fb since RedirectLoginHelper will use the URL parameters set by Facebook.

Return value

\Facebook\Authentication\AccessToken|null User's Facebook access token, if it could be read from Facebook. Null, otherwise.

1 call to FacebookAuthManager::getAccessToken()
FacebookAuthManager::authenticate in src/FacebookAuthManager.php
Authenticates the users by using the access token.

File

src/FacebookAuthManager.php, line 106

Class

FacebookAuthManager
Contains all Simple FB Connect logic that is related to Facebook interaction.

Namespace

Drupal\social_auth_facebook

Code

public function getAccessToken() {
  if (!$this->accessToken) {
    $helper = $this->client
      ->getRedirectLoginHelper();

    // URL where Facebook returned the user.
    $return_url = $this->urlGenerator
      ->generateFromRoute('social_auth_facebook.return_from_fb', [], [
      'absolute' => TRUE,
    ]);
    try {
      $access_token = $helper
        ->getAccessToken($return_url);
    } catch (FacebookResponseException $ex) {

      // Graph API returned an error.
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not get Facebook access token. FacebookResponseException: @message', [
        '@message' => json_encode($ex
          ->getMessage()),
      ]);
      return NULL;
    } catch (FacebookSDKException $ex) {

      // Validation failed or other local issues.
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not get Facebook access token. Exception: @message', [
        '@message' => $ex
          ->getMessage(),
      ]);
      return NULL;
    }

    // If login was OK on Facebook, we now have user's access token.
    if (isset($access_token)) {
      $this->accessToken = $access_token;
    }
    else {

      // If we're still here, user denied the login request on Facebook.
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not get Facebook access token. User cancelled the dialog in Facebook or return URL was not valid.');
      return NULL;
    }
  }
  return $this->accessToken;
}