当我尝试创建Google TextToSpeechClient()的实例时,我得到以下异常:
异常:Could not construct ApplicationDefaultCredentials
既然-我已经在下面添加了phpenv/??还是有问题
putenv('GOOGLE_APPLICATION_CREDENTIALS=/Users/macgowan/google_cloud/service-account-text-to-speech-test-00.json');
下面的文章提到使用google/cloud-text-to-speech而不是我使用的google/cloud-speech。但是代码再次从命令行执行??
为什么我得到一个服务器致命错误时,试图在应用引擎上创建谷歌's类TextToSpeechClient的实例?
谷歌论坛的一些评论https://www.googlecloudcommunity.com/gc/AI-ML/Create-an-instance-of-TextToSpeechClient-and/m-p/421460 M314
任何帮助都是感激的…
composer.json
{
"require": {
"katzgrau/klogger": "dev-master",
"google/cloud": "^0.156.0",
"google/cloud-text-to-speech": "^1.0",
"ext-bcmath" : "*"
}
}
PHP实现(异常:Could not construct ApplicationDefaultCredentials)
<?php
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow- Headers, Authorization, X-Requested-With");
require_once '/home/macgowan/vendor/autoload.php';
// [START tts_synthesize_text]
use GoogleCloudTextToSpeechV1AudioConfig;
use GoogleCloudTextToSpeechV1AudioEncoding;
use GoogleCloudTextToSpeechV1SsmlVoiceGender;
use GoogleCloudTextToSpeechV1SynthesisInput;
use GoogleCloudTextToSpeechV1TextToSpeechClient;
use GoogleCloudTextToSpeechV1VoiceSelectionParams;
putenv('GOOGLE_APPLICATION_CREDENTIALS=/Users/macgowan/google_cloud/service-account-text-to-speech-test-00.json');
try
{
putenv('GOOGLE_APPLICATION_CREDENTIALS=/Users/macgowan/google_cloud/service-account-text-to-speech-test-00.json');
$ip = getenv('GOOGLE_APPLICATION_CREDENTIALS');
printf("Get env var - GOOGLE_APPLICATION_CREDENTIALS: %s<br />", $ip);
$ip = getenv('APACHE_RUN_USER');
printf("Get env var - APACHE_RUN_USER: %s<br />", $ip);
printf("Create the client object from the TextToSpeechClient() class <br />");
$client = new TextToSpeechClient();
printf("Calling: useApplicationDefaultCredentials() <br />");
$client->useApplicationDefaultCredentials();
printf("Client object created sucessfully <br />");
}
catch (Exception $e)
{
printf("Caught exception: %s<br />", $e->getMessage());
// echo 'Caught exception: ', $e->getMessage(), "n";
}
printf("Following Try/Catch on TextToSpeechClient() create <br />");
$text = "Hello Joe";
print('Set input text using the SynthesisInput() object' . PHP_EOL);
$input_text = (new SynthesisInput())
->setText($text);
$voice = (new VoiceSelectionParams())
->setLanguageCode('en-US')
->setSsmlGender(SsmlVoiceGender::FEMALE);
$audioConfig = (new AudioConfig())
->setAudioEncoding(AudioEncoding::MP3);
$response = $client->synthesizeSpeech($input_text, $voice, $audioConfig);
$audioContent = $response->getAudioContent();
file_put_contents('/home/macgowan/output.mp3', $audioContent);
$client->close();
?>
输出Calling: processResults2()
Inside processResults2()
Create the TextToSpeechClient() client object
Get env var - GOOGLE_APPLICATION_CREDENTIALS: /Users/macgowan/google_cloud/service-account-text-to-speech-test-00.json
Get env var - APACHE_RUN_USER: www-data
Create the client object from the TextToSpeechClient() class
Caught exception: Could not construct ApplicationDefaultCredentials
Following Try/Catch on TextToSpeechClient() create
概括一下这个问题的答案:
Google Cloud PHP库使用Service Account凭据连接到Google Cloud服务。在计算引擎上运行时,将自动发现凭据。当在其他环境中运行时,可以通过在环境变量中提供该帐户(或JSON本身)的JSON密钥文件的路径来指定服务帐户凭据。
Google Cloud PHP库按照以下顺序发现凭据:
- 在代码 中指定凭证
- 在环境变量中发现凭据路径
- 发现云SDK路径中的凭证文件
- 发现GCE证书
这些都在这里详细提到。
客户端认证示例:
require __DIR__ . '/vendor/autoload.php';
use GoogleCloudTextToSpeechV1AudioConfig;
use GoogleCloudTextToSpeechV1AudioEncoding;
...
// Authenticating with a keyfile path.
$textToSpeechClient = new TextToSpeechClient([
'keyFilePath' => '/path/to/keyfile.json'
]);
您还可以在PHP客户端引用服务帐户的身份验证。