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

dee*_*waj 3 php google-app-engine google-analytics google-api google-analytics-api

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

Google/Client.php,Google/Service/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);
Run Code Online (Sandbox Code Playgroud)

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

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


DaI*_*mTo 9

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

更新:由于教程仍未更新,我已经制作了可能有用的教程. 谷歌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']; 
?>
Run Code Online (Sandbox Code Playgroud)

  • 那么,什么是`setUseObjects`函数的等价物? (3认同)
  • 我会用新的.有一些样本.附注:GA开发了该教程的问题.他们正在研究新的.但我不能告诉你什么时候他们会活着. (2认同)
  • 7个月后,旧的教程仍在那里,代码仍然会在GitHub的全新安装中生成此错误.瘸 (2认同)
  • 非常感谢.截至2014年12月2日,我可以确认这是有效的! (2认同)