You are here

protected function Client::getToken in OAuth2 Client 7.2

Same name and namespace in other branches
  1. 7 oauth2_client.inc \OAuth2\Client::getToken()

Get and return an access token for the grant_type given in $params.

3 calls to Client::getToken()
Client::getAccessToken in src/Client.php
Get and return an access token.
Client::getTokenRefreshToken in src/Client.php
Get a new access_token using the refresh_token.
Client::getTokenServerSide in src/Client.php
Get an access_token using the server-side (authorization code) flow.

File

src/Client.php, line 435
Class OAuth2\Client.

Class

Client
The class OAuth2\Client is used to communicate with an oauth2 server.

Namespace

OAuth2

Code

protected function getToken($data) {
  if (array_key_exists('scope', $data) && is_null($data['scope'])) {
    unset($data['scope']);
  }
  $client_id = $this->params['client_id'];
  $client_secret = $this->params['client_secret'];
  $token_endpoint = $this->params['token_endpoint'];
  $options = array(
    'method' => 'POST',
    'data' => drupal_http_build_query($data),
    'headers' => array(
      'Content-Type' => 'application/x-www-form-urlencoded',
      'Authorization' => 'Basic ' . base64_encode("{$client_id}:{$client_secret}"),
    ),
  );
  if ($this->params['skip-ssl-verification']) {
    $options['context'] = stream_context_create(array(
      'ssl' => array(
        'verify_peer' => FALSE,
        'verify_peer_name' => FALSE,
      ),
    ));
  }
  $result = drupal_http_request($token_endpoint, $options);
  if ($result->code != 200) {
    throw new \Exception(t("Failed to get an access token of grant_type @grant_type.\nError: @result_error", array(
      '@grant_type' => $data['grant_type'],
      '@result_error' => $result->error,
    )));
  }
  $token = drupal_json_decode($result->data);
  if (!isset($token['expires_in'])) {
    $token['expires_in'] = 3600;
  }
  return $token;
}