You are here

protected function ApiTestBase::getToken in Lightning API 8

Same name and namespace in other branches
  1. 8.2 modules/api_test/tests/src/Functional/ApiTestBase.php \Drupal\Tests\api_test\Functional\ApiTestBase::getToken()

Gets a token from the oauth endpoint for use in requests that require authorization.

Parameters

array $client_options: An array of options to use when requesting the token.

Return value

string The OAuth2 password grant access token from the API.

2 calls to ApiTestBase::getToken()
ApiTest::setUp in modules/api_test/tests/src/Functional/ApiTest.php
EntityCrudTest::setUp in modules/api_test/tests/src/Functional/EntityCrudTest.php

File

modules/api_test/tests/src/Functional/ApiTestBase.php, line 42

Class

ApiTestBase

Namespace

Drupal\Tests\api_test\Functional

Code

protected function getToken($client_options) {
  $client = $this->container
    ->get('http_client');
  $url = $this
    ->buildUrl('/oauth/token');
  $response = $client
    ->post($url, $client_options);
  $body = $this
    ->decodeResponse($response);

  // The response should have an access token.
  $this
    ->assertArrayHasKey('access_token', $body);
  return $body['access_token'];
}