在PHP中使用Google Cloud Translation API时的身份验证问题

发布于 2025-02-08 17:24:12 字数 1232 浏览 2 评论 0原文

我已经尝试在官方文档的帮助下使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2025-02-15 17:24:12

看起来您找不到您的服务帐户密钥文件。

  1. 如果未设置环境变量Google_application_credentials,则ADC使用附加到运行代码的资源的服务帐户。
  2. 该服务帐户可能是Compute Engine,Google Kubernetes引擎,App Engine,Cloud Run或Cloud功能提供的默认服务帐户。它也可能是您创建的用户管理的服务帐户。
  3. 如果ADC无法使用上述任何凭据,则会发生错误。

尝试

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');

It looks like you It cant find your service account key file.

  1. If the environment variable GOOGLE_APPLICATION_CREDENTIALS isn't set, ADC uses the service account that is attached to the resource that is running your code.
  2. This service account might be a default service account provided by Compute Engine, Google Kubernetes Engine, App Engine, Cloud Run, or Cloud Functions. It might also be a user-managed service account that you created.
  3. If ADC can't use any of the above credentials, an error occurs.

Try

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文