php – Google Cloud API – 应用程序默认凭据

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Google Cloud API – 应用程序默认凭据脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码,从 Google’s documentation修改
$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)而不是尝试使用您使用的任何方法($_ENV或$_SERVER).

取自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();

脚本宝典总结

以上是脚本宝典为你收集整理的php – Google Cloud API – 应用程序默认凭据全部内容,希望文章能够帮你解决php – Google Cloud API – 应用程序默认凭据所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。