You are here

protected function Client::getToken in OAuth2 Client 7

Same name and namespace in other branches
  1. 7.2 src/Client.php \OAuth2\Client::getToken()

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

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

File

./oauth2_client.inc, line 426
Class OAuth2\Client

Class

Client
The class OAuth2\Client is used to get authorization from an oauth2 server. Its only goal is to get an access_token from the oauth2 server, so the only public function (besides the constructor) is getAccessToken().

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;
}