protected function GoogleAnalyticsReportsApiFeed::fetchToken in Google Analytics Reports 8.3
Authenticate with the Google Analytics API.
Parameters
string $client_id: Client id.
string $client_secret: Client secret.
string $redirect_uri: Redirect uri.
string $refresh_token: Refresh token.
2 calls to GoogleAnalyticsReportsApiFeed::fetchToken()
- GoogleAnalyticsReportsApiFeed::finishAuthentication in google_analytics_reports_api/
src/ GoogleAnalyticsReportsApiFeed.php - Complete the authentication process.
- GoogleAnalyticsReportsApiFeed::refreshToken in google_analytics_reports_api/
src/ GoogleAnalyticsReportsApiFeed.php - Fetches a fresh access token with the given refresh token.
File
- google_analytics_reports_api/
src/ GoogleAnalyticsReportsApiFeed.php, line 261
Class
- GoogleAnalyticsReportsApiFeed
- Class GoogleAnalyticsReportsApiFeed.
Namespace
Drupal\google_analytics_reports_apiCode
protected function fetchToken($client_id, $client_secret, $redirect_uri, $refresh_token = NULL) {
if ($refresh_token) {
$params = [
'client_id' => $client_id,
'client_secret' => $client_secret,
'refresh_token' => $refresh_token,
'grant_type' => 'refresh_token',
];
}
else {
$current_request_code = $this->requestStack
->getCurrentRequest()->query
->get('code');
$params = [
'code' => $current_request_code,
'grant_type' => 'authorization_code',
'redirect_uri' => $redirect_uri,
'client_id' => $client_id,
'client_secret' => $client_secret,
];
}
try {
$client = new Client();
$response = $client
->post(self::OAUTH2_TOKEN_URI, [
'form_params' => $params,
]);
$this->response = $response
->getBody()
->getContents();
if ($response
->getStatusCode() == '200') {
$decoded_response = json_decode($this->response, TRUE);
$this->accessToken = $decoded_response['access_token'];
$this->expiresAt = time() + $decoded_response['expires_in'];
if (!$refresh_token) {
$this->refreshToken = $decoded_response['refresh_token'];
}
}
else {
$error_vars = [
'@code' => $response
->getStatusCode(),
'@details' => print_r(json_decode($this->response), TRUE),
];
$this->error = $this
->t('<strong>Code</strong>: @code, <strong>Error</strong>: <pre>@details</pre>', $error_vars);
$this->loggerFactory
->error('<strong>Code</strong>: @code, <strong>Error</strong>: <pre>@details</pre>', $error_vars);
}
} catch (ClientException $e) {
$response = $e
->getResponse();
$this->response = $response
->getBody()
->getContents();
$error_vars = [
'@code' => $response
->getStatusCode(),
'@message' => $e
->getMessage(),
'@details' => print_r(json_decode($this->response), TRUE),
];
$this->error = $this
->t('<strong>Code</strong>: @code, <strong>Error</strong>: @message, <strong>Message</strong>: <pre>@details</pre>', $error_vars);
$this->loggerFactory
->error('<strong>Code</strong>: @code, <strong>Error</strong>: <pre>@details</pre>', $error_vars);
}
}