duanlumei5941 2015-03-06 00:28
浏览 28
已采纳

来自localhost的Google AnalyticsAPI

Working on a php script to pull data from Google Analytics with the Google PHP Client API. I've got the script set up at localhost/ga/.

Got my API keys all set up, Analytics API turned on.

In "Client ID for Web application":

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

In "Key for browser application":

REFERERS  http://localhost/*

Double checked all keys,ids, and secrets. But I'm getting this error:

"(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."

My current code:

$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();
    }
}

Any idea what I might be doing wrong?

  • 写回答

1条回答 默认 最新

  • douxi3085 2015-03-06 00:44
    关注

    I figured this out. Bah.

    The Public Key I generated was the browser key not the server key. You can leave the IPs field blank and it will work with localhost.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法