管理中的示例不起作用 API --- 豁免

Dosnt work examples in Management API --- exeptions

Goals: list

None 个示例有效。虽然安装了客户端库。

同时,Google_Service_AnalyticsReporting 正常工作。

<?php
require_once __DIR__ . '/goo/vendor/autoload.php';

$analytics = initializeAnalytics();

$VIEW_ID = "206215735";
$ACCOUNT_ID = '152594033';
$PROPERTY_ID = 'UA-152594033-1';

function initializeAnalytics()
{
    $KEY_FILE_LOCATION = __DIR__ . '/goo/My Project.json';

    $client = new Google_Client();
    $client->setApplicationName("Bottom table");
    $client->setAuthConfig($KEY_FILE_LOCATION);
    $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
    $analytics = new Google_Service_AnalyticsReporting($client);

    return $analytics;
}

    $goals = $analytics->management_goals->listManagementGoals($ACCOUNT_ID,$PROPERTY_ID,'~all');

Result Fatal error: Uncaught Error: Call to a member function listManagementGoals() on null in D:\OpenServer\domains\htdg.local\google1.php:30 Stack trace: #0 {main} thrown in D:\OpenServer\domains\htdg.local\google1.php on line 30

为什么会出现错误

所有示例都有效。您使用了错误的服务。

您的代码正在使用 Google analytics reporting api

的库
$analytics = new Google_Service_AnalyticsReporting($client);

但您正在尝试使用属于 google analytics core reporting api v3 and the managment api 的方法,该方法位于不同的库中。

$analytics = new Google_Service_Analytics($client);

您可能应该遵循 PHP 教程以获得正确的 api Hello Analytics API: PHP quickstart for web applications

$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

$analytics = new Google_Service_Analytics($client);
$goals = $analytics->management_goals->listManagementGoals($ACCOUNT_ID,$PROPERTY_ID,'~all');