2012-05-25 6 views
2

Google 애널리틱스에서 데이터를 가져 오기 위해 전화를 걸려고합니다.Google 애널리틱스 API 오류

<?php 

require_once 'lib/apiClient.php'; 
require_once 'lib/contrib/apiAnalyticsService.php'; 
session_start(); 

$client = new apiClient(); 

$service = new apiAnalyticsService($client); 

if (isset($_GET['logout'])) { 
    unset($_SESSION['token']); 
} 

if (isset($_GET['code'])) { 
    $client->authenticate(); 
    $_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->getAccessToken()) { 
    $accounts = $service->management_accounts->listManagementAccounts(); 
    print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>"; 

    try { 
     $data = $service->data_ga->get('ga:29214712', '2012-01-01', '2012-01-15', 
      'ga:visits', array('dimensions' => 'ga:source,ga:keyword', 'sort' => 
      '-ga:visits,ga:source', 'filters' => 'ga:medium==organic', 'max-results' => '25')); 
    } 
    catch (apiServiceException $e) { 
     echo $e->getCode(); 
     print_r($data); 
    } 

    $_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $authUrl = $client->createAuthUrl(); 
    print "<a class='login' href='$authUrl'>Connect Me!</a>"; 
} 

은 try의 catch 블록의 코드를 포장 한 후 나는 다음과 같은 오류

을 얻고있다

403

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A29214712&start-date=2012-01-01&end-date=2012-01-15&metrics=ga%3Avisits GET 호출 오류 : (!) 금지 (403)

공지 사항 : 정의되지 않은 변수 : 데이터는 입니다. 43 행의 C : \ Wit \ Wit \ gitgrow \ index.php

참고 : 나는 권한을 부여했으며이를 테스트하기 위해 프로필 ID를 사용하도록했습니다.

답변

2

당신이 받고있는 403 오류는 인가 된 사용자이 쿼리에 정의 된 ga : 29214712보고 프로필에 대한 액세스 권한이 없음을 의미합니다.

는 예/분석/데모 디렉토리에 HelloAnalyticsAPI.php 샘플에서 봐, 그리고 당신은 API에 연결할 수 있는지 확인하십시오 또한 http://code.google.com/p/google-api-php-client/source/browse/#svn%2Ftrunk%2Fexamples%2Fanalytics%2Fdemo

는 Google 웹 로그 분석 개발자 가이드를 참조하시기 바랍니다. 올바른 프로필 ID를 얻는 방법을 설명합니다. https://developers.google.com/analytics/devguides/reporting/core/v3/#user_reports

+0

감사합니다. 프로필 ID 대신 계정 ID를 강제로 사용했습니다. –