我在試圖弄清楚如何使用Google API PHP Client來訪問Google Analytics。具體來說,我想要從其他廣告系列upload cost data。我的代碼確實可以從GA獲取信息,我可以查看流量數據的配置文件,但我無法弄清楚如何上傳。Google Analytics(分析)API 401無效憑證
下面是我使用的身份驗證碼:請求數據
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("GA-Tester");
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://www.site.com/indext.php');
$client->setDeveloperKey('xxx');
$analyticsService = new Google_AnalyticsService($client);
$dailyUploads = $analyticsService->management_dailyUploads;
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (!$client->getAccessToken()) {
header ('Location:http://www.site.com/indext.php');
} else {...
此代碼的工作。我可以獲取帳戶,配置文件列表,下載特定配置文件的流量數據等。沒有錯誤或問題。
當我嘗試上傳包含費用數據的CSV文件時,出現'401 Invalid Credentials'錯誤。這裏是發送文件的代碼:
$send = $dailyUploads->upload(
$accountId,
$webPropertyId,
$customDataSourceId,
$start_date,
1,
'cost',
array(
"reset" => true,
"data" => $cont,
"mimeType" => "application/octet-stream",
"uploadType" => "media"));
我已經仔細檢查了所有我通過的變量,他們發送了正確的信息。
所以這是我的問題。如果我所有的GET
請求都沒有問題,爲什麼我的POST
請求會拋出錯誤?我不知道這是否是我的代碼錯誤,或者是API console中的設置。任何人都可以引導我走向正確的方向嗎?
編輯: 這裏的生成的錯誤代碼(減去識別比特)。
Fatal error: Uncaught exception 'Google_ServiceException' with message
'Error calling POST https://www.googleapis.com/upload/analytics/v3/man
agement/accounts/1234567/webproperties/UA-1234567-1/customDataSources/
xXxXxX/dailyUploads/2013-01-17/uploads?appendNumber=1&type=cost&reset=
true&uploadType=media&key=xXxXxX: (401) Invalid Credentials' in /../pub
lic_html/proj/google-api-php-client/src/io/Google_REST.php:66 Stack tra
ce: #0 /../public_html/proj/google-api-php-client/src/io/Google_REST.ph
p(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1/
../public_html/proj/google-api-php-client/src/service/Google_ServiceRes
ource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /..
/public_html/proj/google-api-php-client/src/contrib/Google_AnalyticsSer
vice.php(82): Google_ServiceResource->__call('upload', Array) #3 /../pu
blic_html/proj/dailyUpload_send.php(60): Google_ManagementDailyUploadsS
erviceResource->upload('1234567', 'UA-1234567-1', 'xXxXxXxXxXxXxXx...',
' in /../public_html/proj/google-api-php-client/src/io/Google_REST.php
on line 66
你看過嗎? https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtDailyUploadGuide#upload – 2013-02-20 17:48:38
是的,我的請求是鬆散地基於此代碼。我正在使用PHP庫,因此它看起來不完全相同,但我的代碼包含這些元素中的每一個。 – Lenwood 2013-02-20 18:24:04
您是否授權https://www.googleapis.com/auth/analytics上的授權範圍? – 2013-02-20 18:27:05