You are here

public function BynderApi::initiateOAuthTokenRetrieval in Bynder 8.2

Same name and namespace in other branches
  1. 8.3 src/BynderApi.php \Drupal\bynder\BynderApi::initiateOAuthTokenRetrieval()
  2. 8 src/BynderApi.php \Drupal\bynder\BynderApi::initiateOAuthTokenRetrieval()
  3. 4.0.x src/BynderApi.php \Drupal\bynder\BynderApi::initiateOAuthTokenRetrieval()

Initiates the access token retrieval.

Gets request token from Bynder and prepares everything to redirect user to Bynder for login.

Return value

\Drupal\Core\Url Url to redirect the user to.

Overrides BynderApiInterface::initiateOAuthTokenRetrieval

See also

::finishOAuthTokenRetrieval()

File

src/BynderApi.php, line 163

Class

BynderApi
Bynder API service.

Namespace

Drupal\bynder

Code

public function initiateOAuthTokenRetrieval() {
  $bynder_settings = $this->configFactory
    ->get('bynder.settings');
  $bynder_configuration = [
    'consumerKey' => $bynder_settings
      ->get('consumer_key'),
    'consumerSecret' => $bynder_settings
      ->get('consumer_secret'),
    'baseUrl' => $bynder_settings
      ->get('account_domain'),
    'requestOptions' => [
      'timeout' => $bynder_settings
        ->get('timeout'),
    ],
  ];
  $this->bynderApi = BynderApiFactory::create($bynder_configuration);
  $session_data = $this->session
    ->get('bynder', []);
  foreach (explode('&', $this->bynderApi
    ->getRequestToken()
    ->wait()) as $item) {
    $value = explode('=', $item);
    $session_data['request_token'][$value[0]] = $value[1];
  }
  $this->session
    ->set('bynder', $session_data);
  $callback = Url::fromRoute('bynder.oauth', [], [
    'absolute' => TRUE,
  ])
    ->toString(TRUE)
    ->getGeneratedUrl();
  $query = [
    'oauth_token' => $session_data['request_token']['oauth_token'],
    'callback' => $callback,
  ];
  return Url::fromUri($bynder_settings
    ->get('account_domain') . '/api/v4/oauth/authorise/', array(
    'query' => $query,
    'auth' => null,
    'allow_redirects' => false,
  ));
}