【问题标题】:Google API Vision - Could not load the default credentialsGoogle API Vision - 无法加载默认凭据
【发布时间】:2017-05-03 12:16:49
【问题描述】:
我正在尝试使用 google Vision API。
我做了以下步骤:
- 启用 Google Vision API
- 创建了服务帐号 ID
- 使用 json 路径设置环境变量“GOOGLE_APPLICATION_CREDENTIALS”
- 使用 composer 下载 API
-
写了如下代码:
$vision = new VisionClient([
'projectId' => $googleApiKey,
]);
$image = $vision->image(file_get_contents($path), ['FACE_DETECTION']);
$result = $vision->注释($image);
当我午餐这段代码时,我收到以下错误:
Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message 'Could not load the default credentials.
有人可以帮我解决这个问题吗?
非常感谢!!!
【问题讨论】:
标签:
php
google-app-engine
google-vision
【解决方案1】:
指定时最好使用绝对路径。例如。 /credentials/creds.json 优于 ./creds.json。尝试绝对设置路径。
您可能想尝试从已知的良好配置开始。以下内容可能会有所帮助:
git clone https://github.com/googlecloudplatform/php-docs-samples
cd php-docs-samples/quickstart
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
php composer.phar install
php quickstart.php
```
如果快速入门向您提出有关时区的问题,请将以下内容添加到 quickstart.php 的 require 语句之后:
date_default_timezone_set('America/Los_Angeles');
如果你的路径不正确,你应该会看到错误:
Uncaught exception 'DomainException' with message 'Unable to read the credential file specified by...
使用快速入门示例进行诊断后,您应该弄清楚代码中发生了什么。