【发布时间】:2018-06-14 19:22:16
【问题描述】:
我正在尝试制作一个 PHP 应用程序,使用它我可以将 .flac 文件发送到 google 语音服务并获取文本作为回报。
这是验证码:
require 'vendor/autoload.php';
use Google\Cloud\Core\ServiceBuilder;
// Authenticate using keyfile data
$cloud = new ServiceBuilder([
'keyFile' => json_decode(file_get_contents('b.json'), true)
]);
那么这是语音代码:
use Google\Cloud\Speech\SpeechClient;
$speech = new SpeechClient([
'languageCode' => 'en-US'
]);
// Recognize the speech in an audio file.
$results = $speech->recognize(
fopen(__DIR__ . '/audio_sample.flac', 'r')
);
foreach ($results as $result) {
echo $result->topAlternative()['transcript'] . "\n";
}
当然 $cloud 变量从未被使用过。应该去哪里?
我跑了还是得到了
未捕获的异常“Google\Cloud\Core\Exception\ServiceException” 带有消息'{“错误”:{“代码”:401,“消息”:“请求已 无效的身份验证凭据。预期的 OAuth 2 访问令牌, 登录 cookie 或其他有效的身份验证凭据。看 https://developers.google.com/identity/sign-in/web/devconsole-project.", “状态”:“未经身份验证”} }
我只想做一个简单的查询。任何帮助将不胜感激。
【问题讨论】:
-
您是否创建了密钥文件?
-
@rtfm:是的,b.json 是一个有效的密钥文件。已按照以下链接创建它:developers.google.com/identity/protocols/…
标签: php google-cloud-platform google-cloud-speech