2

我試圖運行谷歌API嵌入服務器端授權的演示,在這裏:https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/,但使用PHP,而不是Python的3步(使用JSON關鍵數據來請求訪問令牌)。如何使用PHP運行Google Embed API服務器端授權?

我已經安裝了谷歌API PHP客戶端:https://github.com/google/google-api-php-client

我創建了服務帳戶(https://console.developers.google.com),爲其啓用了Analytics API,下載了JSON密鑰文件,並將其複製到我的Web服務器(爲了安全起見,位於Web根目錄下)。

我已經註冊了服務帳戶在谷歌Analytics帳戶(讀&只分析)。

我嘗試使用替代PHP代碼從這裏https://stackoverflow.com/a/32767845/1803239運行演示:

<?php  

// Load the Google API PHP Client Library. 
require_once 'google-api-php-client/src/Google/autoload.php'; 

// Start a session to persist credentials. 
session_start(); 

// Create the client object and set the authorization configuration 
// from the client_secretes.json you downloaded from the developer console. 
$client = new Google_Client(); 
$client->setAuthConfigFile('/path/to/client_secrets.json'); 
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY); 


// If the user has already authorized this app then get an access token 
// else redirect to ask the user to authorize access to Google Analytics. 
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
    // Set the access token on the client. 
    $client->setAccessToken($_SESSION['access_token']); 

    // Create an authorized analytics service object. 
    $analytics = new Google_Service_Analytics($client); 


    // Get the results from the Core Reporting API and print the results. 

} else { 
    $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'; 
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 
} 


//get the access token 
$myToken = json_decode($client->getAccessToken()); 
?> 

運行演示(在一個文件名爲test3.php,用PHP代碼,而不是Python代碼)在失敗這條線, $client->setAuthConfigFile('client_secrets.json');, ,出現以下錯誤:

Fatal error: Uncaught exception 'Google_Exception' with message 'Invalid client secret JSON file.' in /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php:171 Stack trace:

#0 /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php(189): Google_Client->setAuthConfig('{? "type": "se...')

#1 /var/www/vhosts/mydomain.com/httpdocs/test3.php(36): Google_Client->setAuthConfigFile('/var/www/vhosts...')

#2 {main} thrown in /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php on line 171

任何人都可以說明爲什麼我的JSON密鑰文件可能會失敗?

更新:OK,我一直在努力,現在得到這個工作5天。也許它可以發揮作用,但我對Google文檔的信心很糟糕。谷歌,如果你正在閱讀這篇文章,請整理你的文檔,他們是極端sl sl。對他們進行測試。

如果任何人都可以提供API服務器端授權演示(https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/)的實際示例,但在步驟3中使用PHP而不是Python,我將永遠感激不盡。謝謝。

+0

因爲你餵養它一個服務帳戶JSON文件,而不是一個的oauth2 JSON文件。您的代碼適用於Oauth2而不是服務帳戶。 – DaImTo

+0

演示說明確實會說「步驟1:創建服務帳戶並下載JSON密鑰」。他們可能是錯的,還是我在這裏使用錯誤的PHP代碼? – Gavin

+1

您正在使用錯誤的php代碼。 https://developers.google.com/api-client-library/php/auth/service-accounts – DaImTo

回答

2

這裏是我最終做這

$scope = 'https://www.googleapis.com/auth/analytics.readonly'; 
$jsonKey = json_decode('[json file content here]', true); 
// OR json key file path 
// $jsonKeyFilePath = '/full/path/to/service-account.json'; 

$client = new \Google_Client(); 
$client->setScopes([$scope]); 
$client->setAuthConfig($jsonKey); 

// OR use json file 
// $client->setAuthConfigFile($jsonKeyFilePath); 

$client->useApplicationDefaultCredentials(); 
$client->fetchAccessTokenWithAssertion(); 

echo $client->getAccessToken(); 

在谷歌API客戶端V2也有一些更新 https://github.com/google/google-api-php-client/blob/master/UPGRADING.md

,截至2016年9月都沒有在文檔尚未更新(其中包含HOWTO對於V1) https://developers.google.com/api-client-library/php/auth/service-accounts