我一直面临着一个持续的问题,而试图设置谷歌分析数据API(GA 4)使用谷歌提供的快速入门指南(快速入门指南)。
当我设置GOOGLE_APPLICATION_CREDENTIALS
时出现了特定的问题。我已经正确地将credentials.json
文件放在quickstart.php
文件旁边,并使用PHP putenv
函数设置变量,如下所示:
putenv('GOOGLE_APPLICATION_CREDENTIALS="'.__DIR__.'/credentials.json"');
不幸的是,每次我运行我的测试时,我都会遇到这个错误消息:
Fatal error: Uncaught DomainException: Unable to read the credential file specified by GOOGLE_APPLICATION_CREDENTIALS: file "/google/credentials.json" does not exist in /google/vendor/google/auth/src/CredentialsLoader.php:81 Stack trace: #0 /google/vendor/google/auth/src/ApplicationDefaultCredentials.php(160): Google\Auth\CredentialsLoader::fromEnv() #1 /google/vendor/google/gax/src/CredentialsWrapper.php(267): Google\Auth\ApplicationDefaultCredentials::getCredentials() #2 /google/vendor/google/gax/src/CredentialsWrapper.php(136): Google\ApiCore\CredentialsWrapper::buildApplicationDefaultCredentials() #3 /google/vendor/google/gax/src/GapicClientTrait in /google/vendor/google/gax/src/CredentialsWrapper.php on line 275
我三次检查了文件路径,一切似乎都很正常,但错误仍然存在。
有没有人遇到过这个问题,或者有没有人对如何解决这个问题有见解?任何帮助都非常感谢。
附加信息:
- 语言:PHP
- API:Google Analytics数据API(GA 4)
在此先谢谢您!
参考代码:
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
date_default_timezone_set('Europe/London');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
putenv('GOOGLE_APPLICATION_CREDENTIALS="'.__DIR__.'/credentials.json"');
require 'vendor/autoload.php';
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
/**
* TODO(developer): Replace this variable with your Google Analytics 4
* property ID before running the sample.
*/
$property_id = 'YOUR-GA4-PROPERTY-ID';
// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();
// Make an API call.
$response = $client->runReport([
'property' => 'properties/' . $property_id,
'dateRanges' => [
new DateRange([
'start_date' => '2020-03-31',
'end_date' => 'today',
]),
],
'dimensions' => [new Dimension(
[
'name' => 'city',
]
),
],
'metrics' => [new Metric(
[
'name' => 'activeUsers',
]
)
]
]);
// Print results of an API call.
print 'Report result: ' . PHP_EOL;
foreach ($response->getRows() as $row) {
print $row->getDimensionValues()[0]->getValue()
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}
2条答案
按热度按时间qgelzfjb1#
正如你可以猜到的,问题是它找不到文件,或者你的Web服务器无法访问你放置的文件。
您可以强制馈送它并绕过env var进行测试,或者完全绕过它
axzmvihb2#
应该是:
非:
问题似乎在于: