我一直在尋找並查找這個爲期兩天的時間,並且無法弄清楚我哪裏出錯了。我試圖從我的谷歌分析帳戶使用PHP拉數據。我已按照所有步驟設置了服務帳戶並下載了JSON文件。這裏是我使用的代碼:使用PHP和服務帳戶的Google AnalyticsAPI
// 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表中,但我並不擔心這一部分,因爲我仍然試圖通過第一個障礙。任何幫助,將不勝感激!
,並且使用的是這裏哪個庫? –
致命錯誤:未收到的異常'Google_Exception'帶消息'無效的客戶機密鑰JSON文件'。 in /home/acclients/google-api-php-client-1-master/src/Google/Client.php:171堆棧跟蹤:#0 /public_html/dashboard.php(27):Google_Client-> setAuthConfig('/ home/acclient/...')#1 {main}扔在/google-api-php-client-1-master/src/Google/Client.php 171行上 該庫是代碼的第一行發佈在我的問題 –
你確定存在'/ inc/ac_key.json'嗎? –