ReportController.php 1.36 KB
<?php
/**
 * Created by PhpStorm.
 * User: Alex Savenko
 * Date: 28.12.2016
 * Time: 21:47
 */

namespace backend\controllers;

use yii\web\Controller;


class ReportController extends Controller
{
    public function actionIndex() {

        return $this->render('index', [
        ]);
    }

    public function actionCallback() {
        // Создание объекта клиента и установка конфигурации авторизации
        // из файла client_secrets.json, скачанного из Developers Console.
        $client = new Google_Client();
        $client->setAuthConfig(__DIR__ . '/client_secrets.json');
        $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/admin/report/callback');
        $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

        // Выполнение процесса авторизации с сервера.
        if (! isset($_GET['code'])) {
            $auth_url = $client->createAuthUrl();
            header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
        } else {
            $client->authenticate($_GET['code']);
            $_SESSION['access_token'] = $client->getAccessToken();
            $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
            header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
        }
    }
}