Google 分析 API 使用 PHP 和服务帐户

Google Analytics API using PHP with service account

我这两天一直在搜索和查找,但不知道我哪里出错了。我正在尝试使用 php 从我的 google 分析帐户中提取数据。我已按照所有步骤设置服务帐户并下载了 JSON 文件。这是我使用的代码:

// Load the Google API PHP Client Library
require_once ('/google-api-php-client-1-master/src/Google/autoload.php');
$KEY_FILE_LOCATION = '/inc/ac_key.json';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_Analytics($client);
function getResults($analytics, $profileId) {
  // Calls the Core Reporting API and queries for the number of sessions
  // for the last seven days.
   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '30daysAgo',
       'today',
       'ga:pageviews'
      );
}
$profile = $r['google_analytics'];
$results = getResults($analytics, $profile);
$month = date('n');
$year = date('Y');
$results->setMonth($month,$year);
$visits = $results->getVisitors();
$views = $results->getPageviews();
/* build tables */
if(count($visits)) {
    foreach($visits as $day=>$visit) { 
        $flot_datas_visits[] = '['.$day.','.$visit.']';
        $flot_datas_views[] = '['.$day.','.$views[$day].']';
    }
    $flot_data_visits = '['.implode(',',$flot_datas_visits).']';
    $flot_data_views = '['.implode(',',$flot_datas_views).']';
}

我收到无效 client_secret.json 文件的错误,但我正在尝试使用服务帐户进行身份验证,所以我不确定这里发生了什么。然后我试图在一个 flot table 中绘制数据,但我并不担心那部分,因为我仍在努力克服第一个障碍。如有任何帮助,我们将不胜感激!

我不是很了解 Google PHP API 客户端,但显然提供了不同的版本 - 当我克隆我得到的 repo 时您应用程序使用的版本,但我没有让它与我的凭据文件一起使用。然后我通过 composer 安装了当前版本的库:

composer require google/apiclient:^2.0

然后我可以使用以下代码访问 Google 分析:

<?php
include_once __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('America/Los_Angeles');
$client = new Google_Client();

$client->setAuthConfig('/path/to/credentials.json');

$client->setApplicationName("Client_Library_Examples");
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$service = new Google_Service_Analytics($client);

function getResults($service, $profileId) {
   return $service->data_ga->get(
       'ga:' . $profileId,
       '30daysAgo',
       'today',
       'ga:pageviews'
      );
}

$profile= "80493610";
$results = getResults($service, $profile);

print_r($results);

经过一番摸索,我设法从您的示例中获取了一个包含旧版本的分析服务实例。显然你必须使用 loadServiceAccountJson 函数而不是 setAuthConfig

<?php

// Load the Google API PHP Client Library
require_once ('google-api-php-client/src/Google/autoload.php');
$KEY_FILE_LOCATION =  '/path/to/credentials.json';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$scopes = ['https://www.googleapis.com/auth/analytics.readonly'];
$client->loadServiceAccountJson($KEY_FILE_LOCATION, $scopes);

$analytics = new Google_Service_Analytics($client);

print_r($analytics);