You are here

function yandex_metrics_oauth_callback in Yandex.Metrics 7

Same name and namespace in other branches
  1. 6 yandex_metrics.module \yandex_metrics_oauth_callback()

Menu callback. Perform auth token request by code parameter from url. More information at (Russian): http://api.yandex.ru/oauth/doc/dg/reference/obtain-access-token.xml

1 string reference to 'yandex_metrics_oauth_callback'
yandex_metrics_menu in ./yandex_metrics.module
Implementation of hook_menu().

File

./yandex_metrics.module, line 776
The main code of Yandex.Metrics module.

Code

function yandex_metrics_oauth_callback() {
  if (empty($_GET['code'])) {
    watchdog('yandex_metrics', 'The "code" parameter is empty.', array(), WATCHDOG_WARNING);
    drupal_set_message(t('An error has occurred. Please try again.'), 'error');
    drupal_goto('admin/config/system/yandex_metrics/authorization');
  }
  $client_secret = variable_get('yandex_metrics_client_secret', '');
  $client_id = variable_get('yandex_metrics_client_id', '');
  $data = 'grant_type=authorization_code&client_id=' . $client_id . '&code=' . $_GET['code'];
  if (!empty($client_secret)) {
    $data .= '&client_secret=' . $client_secret;
  }
  $result = drupal_http_request("https://oauth.yandex.ru/token", array(
    'method' => 'POST',
    'data' => $data,
  ));
  if (isset($result->error)) {
    watchdog('yandex_metrics', 'Token request seems to be fail, due to "%error".', array(
      '%error' => $result->code . ' ' . $result->error,
    ), WATCHDOG_WARNING);
    drupal_set_message(t('An error has occurred. Please try again.'), 'error');
    drupal_goto('admin/config/system/yandex_metrics/authorization');
  }
  $response = json_decode($result->data);
  variable_set('yandex_metrics_auth_token', $response->access_token);
  watchdog('yandex_metrics', 'Token request is successful.');
  drupal_set_message(t('Congratulations! Your application has been authorized by Yandex.Metrics service.'));
  drupal_goto('admin/config/system/yandex_metrics/authorization');
}