Google Analytics Data API (GA4) 设置 - 尽管路径正确,但仍无法读取凭据文件

问题描述 投票:0回答:1

我在尝试使用 Google 提供的快速入门指南(快速入门)设置 Google Analytics Data API (GA4) 时遇到了一个持续存在的问题。

当我设置

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 (GA4)

提前谢谢您!

php google-analytics google-api google-analytics-api google-api-php-client
1个回答
0
投票

您可以猜到问题是它无法找到该文件,或者您的网络服务器无法访问您放置该文件的位置。

您可以强制输入它并绕过环境变量进行测试,或者完全绕过它

// Authenticate using a keyfile path
$client = new BetaAnalyticsDataClient([
    'credentials' => $service_account_key_file_path
]);
© www.soinside.com 2019 - 2024. All rights reserved.