2017-05-03 31 views
0

我正在嘗試使用Google Vision API。 我做了以下步驟:Google API Vision - 無法加載默認憑據

  1. 啓用了谷歌視覺API
  2. 創建服務帳戶ID
  3. 設置好的的是交流電varialbe 'GOOGLE_APPLICATION_CREDENTIALS' 與JSON路徑
  4. 下載的API與作曲家
  5. 寫下了以下代碼:

    $vision = new VisionClient([ 
        'projectId' => $googleApiKey, 
    ]); 
    

    $ image = $ vision-> image(file_get_contents($ path),['FACE_DETECTION']); $ result = $ vision-> annotate($ image);

當我的午餐這個代碼,我得到了以下錯誤:

Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message 'Could not load the default credentials. 

有人可以幫我解決這個問題呢?

非常感謝!

回答

0

指定時最好使用絕對路徑。例如。 /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後需要聲明:

date_default_timezone_set('America/Los_Angeles');

如果您的路徑不正確,您應該看到以下錯誤:

Uncaught exception 'DomainException' with message 'Unable to read the credential file specified by... 

用快速入門示例進行診斷後,您應該弄清楚代碼中發生了什麼。