Google Cloud API-应用程序默认凭据 [英] Google Cloud API - Application Default Credentials
问题描述
我有以下代码,是从 Google的文档修改的: /p>
I have the following code, modified from 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');
但是,当我运行它时,它会返回错误:
Yet when I run it it returns the error:
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'
我尝试了各种配置,例如更改文件的路径.如您所见,我还完成了我可以立即想到的三种不同形式的变量(两种环境,一种没有).
I have tried various configurations, for example changing the path of the file. As you see, I've also done the three different forms of variables that I could immediately think of (two environment, one not).
我不太确定下一步要去哪里.我应该研究设置环境变量的不同方法还是应该以不同的方式定义路径?正确的做法是什么?还有其他错误原因吗?
I'm not quite sure where to look next. Should I look into different ways of setting the environment variable or should I define the path in a different way? What are the correct ways of doing this? Is there any other reason for the error?
推荐答案
您需要使用putenv()
( http ://php.net/manual/zh-CN/function.putenv.php ),而不是尝试使用已使用的任何方法($_ENV
或$_SERVER
).
You need to use putenv()
(http://php.net/manual/en/function.putenv.php) instead of trying to use any of the methods you have used ($_ENV
or $_SERVER
).
// OR use environment variables (recommended)
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client->useApplicationDefaultCredentials();
这篇关于Google Cloud API-应用程序默认凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!