在PHP中使用Google Cloud Translation API时的身份验证问题
我已经尝试在官方文档的帮助下使用Google Translation API,但是我使用支持词汇表功能的版本3遇到了问题。下面的代码返回以下错误,其中我在'[project_id]'中使用了个人projectID。
我发现的一些答案建议使用JSON文件的 gcloud auth应用程序默认登录命令,但不幸的是,它对我不起作用。除了使用GCloud命令外,还有其他用于身份验证的解决方案吗?
未识别的域Exception:无法加载默认凭据。浏览到 https://developers.google.com/accounts/accounts/docs/docs/application--com- default-credentials 有关/home/user/production/test.org/vendor/google/auth/auth/src/applicationdefaultcredentials.php:189 stack trace:#0
<?php
require 'vendor/autoload.php';
use Google\Cloud\Translate\V3\TranslationServiceClient;
$translationClient = new TranslationServiceClient();
$content = ['one', 'two', 'three'];
$targetLanguage = 'es';
$response = $translationClient->translateText(
$content,
$targetLanguage,
TranslationServiceClient::locationName('[PROJECT_ID]', 'global')
);
foreach ($response->getTranslations() as $key => $translation) {
$separator = $key === 2
? '!'
: ', ';
echo $translation->getTranslatedText() . $separator;
}
?>
I've tried using the google translation API with the help of the official document, but I encountered a problem using version 3 that supports a glossary function. The code below returns the following error where I used my personal projectId inside '[PROJECT_ID]'.
Some of the answers I found suggested using the gcloud auth application-default login command with json file but unfortunately, it didn't work for me. Is there an alternative solution for authentication other than using the gcloud command?
Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /home/user/production/test.org/vendor/google/auth/src/ApplicationDefaultCredentials.php:189 Stack trace: #0
<?php
require 'vendor/autoload.php';
use Google\Cloud\Translate\V3\TranslationServiceClient;
$translationClient = new TranslationServiceClient();
$content = ['one', 'two', 'three'];
$targetLanguage = 'es';
$response = $translationClient->translateText(
$content,
$targetLanguage,
TranslationServiceClient::locationName('[PROJECT_ID]', 'global')
);
foreach ($response->getTranslations() as $key => $translation) {
$separator = $key === 2
? '!'
: ', ';
echo $translation->getTranslatedText() . $separator;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您找不到您的服务帐户密钥文件。
尝试
It looks like you It cant find your service account key file.
Try