我绞尽脑汁试图找出如何使用 Google API PHP Client 访问 Google Analytics(分析)。具体来说,我想从其他事件中删除。我的代码确实可以从 GA 获取信息,我可以查看个人资料的流量数据,但我不知道如何上传。
这是我用于身份验证的代码:
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 无效凭据”错误。这是发送文件的代码:
$send = $dailyUploads->upload(
$accountId,
$webPropertyId,
$customDataSourceId,
$start_date,
1,
'cost',
array(
"reset" => true,
"data" => $cont,
"mimeType" => "application/octet-stream",
"uploadType" => "media"));
我已经仔细检查了我传递的所有变量,它们发送了正确的信息。
这是我的问题。如果我的所有 GET
请求都正常工作,为什么我的 POST
请求会抛出错误?我无法判断这是否是我的代码的错误,或者 upload cost data 中的设置的错误。谁能引导我走向正确的方向?
编辑: 这是生成的错误代码(减去可识别位)。
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
最佳答案
我刚刚第一次成功上传成本数据。在交易评论中 Nicholas Pickering我发现了 Management API Authorization 的文档其中指出:
You will get a
401
status code if youraccess_token
has expired or if you are using the wrong scope for the API.
范围的默认值是只读的,这就是为什么我的所有 GET
请求都可以毫无困难地工作,但我无法通过 API 完成任何 POST
请求。我不记得我是如何找到它的,但在 PHP 客户端库中完成此操作的方法是将以下行附加到客户端声明中:
$client->setScopes('https://www.googleapis.com/auth/analytics');
当我将该行添加到我的项目中后,我就能够成功上传成本数据。我真的很高兴克服了这个障碍。我还有很长的路要走才能完成我的项目,但至少我知道它会成功。
关于php - Google Analytics API 401 无效凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14986394/