You are here

public function Hubspot::authorize in HubSpot 8

Authorize site via OAuth.

Parameters

string $code: Auth authorization code.

Throws

\GuzzleHttp\Exception\GuzzleException

File

src/Hubspot.php, line 137

Class

Hubspot
Define a service for interacting with the HubSpot CRM.

Namespace

Drupal\hubspot

Code

public function authorize(string $code) {
  $response = $this->httpClient
    ->post('https://api.hubapi.com/oauth/v1/token', [
    'headers' => [
      'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
    ],
    'form_params' => [
      'grant_type' => 'authorization_code',
      'client_id' => $this->config
        ->get('hubspot_client_id'),
      'client_secret' => $this->config
        ->get('hubspot_client_secret'),
      'redirect_uri' => Url::fromRoute('hubspot.oauth_connect', [], [
        'absolute' => TRUE,
      ])
        ->toString(),
      'code' => $code,
    ],
  ]);
  $data = Json::decode($response
    ->getBody()
    ->getContents());
  $this->state
    ->set('hubspot.hubspot_access_token', $data['access_token']);
  $this->state
    ->set('hubspot.hubspot_refresh_token', $data['refresh_token']);
  $this->state
    ->set('hubspot.hubspot_expires_in', $data['expires_in'] + $this->currentRequest->server
    ->get('REQUEST_TIME'));
}