我有以下代码,修改自谷歌的文档 https://developers.google.com/identity/protocols/application-default-credentials:
$GOOGLE_APPLICATION_CREDENTIALS = "./[path].json";
$_ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "./[path].json";
$_SERVER["GOOGLE_APPLICATION_CREDENTIALS"] = "./[path].json";
$projectId = "[my project's ID']";
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/books']);
$service = new Google_Service_Books($client);
$results = $service->volumes->listVolumes('Henry David Thoreau');
然而,当我运行它时,它返回错误:
PHP Fatal error: Uncaught exception 'DomainException' with message 'Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information'
我尝试了各种配置,例如更改文件的路径。正如您所看到的,我还完成了我可以立即想到的三种不同形式的变量(两种环境,一种不是)。
我不太确定下一步该看哪里。我应该研究设置环境变量的不同方法还是应该以不同的方式定义路径?这样做的正确方法是什么?错误还有其他原因吗?
你需要使用putenv()
(http://php.net/manual/en/function.putenv.php http://php.net/manual/en/function.putenv.php)而不是尝试使用您使用过的任何方法($_ENV
or $_SERVER
).
取自https://github.com/google/google-api-php-client/blob/master/UPGRADING.md#google_auth_assertioncredentials-has-been-removed https://github.com/google/google-api-php-client/blob/master/UPGRADING.md#google_auth_assertioncredentials-has-been-removed
// OR use environment variables (recommended)
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client->useApplicationDefaultCredentials();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)