You are here

public function TwitterAuthController::getProfile in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php \Drupal\social_auth_twitter\Controller\TwitterAuthController::getProfile()
  2. 8.2 modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php \Drupal\social_auth_twitter\Controller\TwitterAuthController::getProfile()
  3. 8.3 modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php \Drupal\social_auth_twitter\Controller\TwitterAuthController::getProfile()
  4. 8.4 modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php \Drupal\social_auth_twitter\Controller\TwitterAuthController::getProfile()
  5. 8.5 modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php \Drupal\social_auth_twitter\Controller\TwitterAuthController::getProfile()
  6. 8.6 modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php \Drupal\social_auth_twitter\Controller\TwitterAuthController::getProfile()
  7. 8.7 modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php \Drupal\social_auth_twitter\Controller\TwitterAuthController::getProfile()
  8. 8.8 modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php \Drupal\social_auth_twitter\Controller\TwitterAuthController::getProfile()

Loads access token, then loads profile.

Parameters

string $type: The type.

Return value

object Returns an object.

2 calls to TwitterAuthController::getProfile()
TwitterAuthController::userLoginCallback in modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php
Authorizes the user after redirect from Twitter.
TwitterAuthController::userRegisterCallback in modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php
Registers the new account after redirect from Twitter.

File

modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php, line 261

Class

TwitterAuthController
Manages requests to Twitter API.

Namespace

Drupal\social_auth_twitter\Controller

Code

public function getProfile($type) {
  $sdk = $this
    ->getSdk($type);
  $data_handler = $this->networkManager
    ->createInstance('social_auth_twitter')
    ->getDataHandler();

  // Get the OAuth token from Twitter.
  if (!($oauth_token = $data_handler
    ->get('oauth_token')) || !($oauth_token_secret = $data_handler
    ->get('oauth_token_secret'))) {
    drupal_set_message($this
      ->t('@network login failed. Token is not valid.', [
      '@network' => $this
        ->t('Twitter'),
    ]), 'error');
    return $this
      ->redirect('user.' . $type);
  }
  $this->authManager
    ->setAccessToken([
    'oauth_token' => $oauth_token,
    'oauth_token_secret' => $oauth_token_secret,
  ]);

  // Gets the permanent access token.
  $this->accessToken = $sdk
    ->oauth('oauth/access_token', [
    'oauth_verifier' => $this->request
      ->get('oauth_verifier'),
  ]);
  $this->authManager
    ->setAccessToken([
    'oauth_token' => $this->accessToken['oauth_token'],
    'oauth_token_secret' => $this->accessToken['oauth_token_secret'],
  ]);

  // Get user's profile from Twitter API.
  if (!($profile = $this->authManager
    ->getProfile()) || !$this->authManager
    ->getAccountId()) {
    drupal_set_message($this
      ->t('@network login failed, could not load @network profile. Contact the site administrator.', [
      '@network' => $this
        ->t('Twitter'),
    ]), 'error');
    return $this
      ->redirect('user.' . $type);
  }
  return $profile;
}