使用HelloAnalyticsApi时,在Google Analytics中调用未定义的方法Google_Client

时间:2014-03-13 06:40:05

标签: php google-app-engine google-analytics google-api google-analytics-api

我正在使用Core Reporting API进行报告。我在我的localhost服务器上安装了Google PHP API客户端主服务器,并在src文件夹中创建了一个文件HelloAnalyticsAPi.php,其中包括

  

谷歌/ Client.php    ,   谷歌/服务/ Analytics.php

的文件。并使用以下详细信息

$client->setClientId('XXXXXXXXXXX.apps.googleusercontent.com'); 
$client->setClientSecret('XXXXXXXXXXX'); 
$client->setRedirectUri('http://localhost/analytics/src/HelloAnalyticsApi.php'); 
$client->setDeveloperKey('XXXXXXXXXXX'); 
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$client->setUseObjects(true);

我在 setUseObjects 上有致命错误。错误是Fatal error: Call to undefined method Google_Client::setUseObjects()。我也在谷歌分析后端做了一些授权。

请告诉我在我的服务器上获取报告的整个过程。因为我无法理解他们给出的谷歌分析的开发者指南。


1 个答案:

答案 0 :(得分:8)

您遇到的问题是您的客户端lib错误。 Hello Analytics API教程是使用Code.google - google-api-php-client 上的旧库创建的,而不是github上的新版本。

更新: 由于教程仍然没有更新,我已经制作了一个可能有用的教程。 Google Oauth2 php。下面的代码直接从它中删除。本教程将保持最新,您可能需要检查是否有任何更改。

<?php         
require_once 'Google/Client.php';     
require_once 'Google/Service/Analytics.php';       
session_start();      
$client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    $client->setDeveloperKey("{devkey}");  
    $client->setClientId('{clientid}.apps.googleusercontent.com');
    $client->setClientSecret('{clientsecret}');
    $client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
    $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

    //For loging out.
    if ($_GET['logout'] == "1") {
    unset($_SESSION['token']);
       }   

    // Step 2: The user accepted your access now you need to exchange it.
    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));
    }

    // Step 1:  The user has not authenticated we give them a link to login    
    if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
        $authUrl = $client->createAuthUrl();
        print "<a class='login' href='$authUrl'>Connect Me!</a>";
        }        

    // Step 3: We have access we can now create our service
    if (isset($_SESSION['token'])) {
        print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
        $client->setAccessToken($_SESSION['token']);
        $service = new Google_Service_Analytics($client);    

        // request user accounts
        $accounts = $service->management_accountSummaries->listManagementAccountSummaries();

       foreach ($accounts->getItems() as $item) {
        echo "Account: ",$item['name'], "  " , $item['id'], "<br /> \n";        
        foreach($item->getWebProperties() as $wp) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> \n";    

            $views = $wp->getProfiles();
            if (!is_null($views)) {
                foreach($wp->getProfiles() as $view) {
                //  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> \n";    
                }
            }
        }
    } // closes account summaries

    }
 print "<br><br><br>";
 print "Access from google: " . $_SESSION['token']; 
?>