使用Google Analytics API

时间:2017-02-09 10:05:16

标签: php google-api google-oauth google-analytics-api google-api-php-client

我在获取帐户用户列表时遇到问题

如果我将帐号放在试用页面(https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountUserLinks/list)中,则可以使用

$client = new Google_Client();
$client->setAuthConfigFile($_SERVER['DOCUMENT_ROOT'] . '/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_MANAGE_USERS);

$client=gaGetClient();
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
     $client->setAccessToken($_SESSION['access_token']);
     $analytics = new Google_Service_Analytics($client);
     //this doesn't work
     $accountUserlinks=$analytics->management_accountUserLinks->listManagementAccountUserLinks('123456');
     //this works
     $man_accounts = $analytics->management_accounts->listManagementAccounts();
      $accounts = [];
      foreach ($man_accounts['items'] as $account) {
           $accounts[] = [ 'id' => $account['id'], 'name' => $account['name'] ];
           echo $account['name'] . " (" . $account['id'] . ")<br/>";
      }
}

我收到此错误消息

  

致命错误:未捕获的异常'Google_Service_Exception'   消息'调用GET时出错   https://www.googleapis.com/analytics/v3/management/accounts/123456/entityUserLinks:   (403)“权限不足”   /home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php:110   堆栈跟踪:#0   /home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php(62):   Google_Http_REST :: decodeHttpResponse(对象(Google_Http_Request)   对象(Google_Client))#1 [内部函数]:   Google_Http_REST :: doExecute(对象(Google_Client)   对象(Google_Http_Request))#2   /home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Task/Runner.php(174):   call_user_func_array(Array,Array)#3   /home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php(46):   Google_Task_Runner-&gt; run()#4   /home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Goo   在   /home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php   在线110

有谁能想到我做错了什么。我认为这是一个范围问题,但我已经说明了我的范围

由于

捐赠

0 个答案:

没有答案