Google Analytics API от localhost

0

Работа с php-скриптом для переноса данных из Google Analytics с помощью API-интерфейсов Google PHP. У меня есть скрипт, настроенный на localhost/ga/.

У меня установлены все ключи API, включен API Google Analytics.

В "Идентификатор клиента для веб-приложения":

REDIRECT URIS  http://localhost/ga/
JAVASCRIPT ORIGINS http://localhost

В "Ключ для приложения браузера":

REFERERS  http://localhost/*

Двойная проверка всех ключей, идентификаторов и секретов. Но я получаю эту ошибку:

"(403) There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."

Мой текущий код:

$client = new Google_Client();
$client->setApplicationName('GA Test');
$client->setClientId($cred['id']);
$client->setClientSecret($cred['secret']);
$client->setRedirectUri($cred['redirect']);
$client->setDeveloperKey($cred['api_key']);
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}

if($client->isAccessTokenExpired()) {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
}

if (!$client->getAccessToken()) {
    $authUrl = $client->createAuthUrl();
    echo '<a class="login" href="'.$authUrl.'">Connect Me!</a>';
} else {
    $analytics = new Google_Service_Analytics($client);
    try {
        $optParams = array(
            'dimensions' => 'ga:source,ga:keyword',
            'sort' => '-ga:sessions,ga:source',
            'filters' => 'ga:medium==organic',
            'max-results' => '25'
        );
        $dump =  $analytics->data_ga->get(
            'ga:52371304',
            '2015-01-01',
            '2015-03-01',
            'ga:sessions',
            $optParams
        );
        var_dump($dump);
        echo 'hi!';
    } catch(Exception $e) {
        echo $e->getMessage();
    }
}

Любая идея, что я могу делать неправильно?

Теги:
google-api
google-analytics
google-analytics-api
google-api-php-client

1 ответ

0
Лучший ответ

Я понял это. Ба.

Открытый ключ, который я сгенерировал, был ключом браузера, а не ключом сервера. Вы можете оставить поле IP-адресов пустым, и оно будет работать с localhost.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню