我正在尝试在Laravel项目中使用Google Translate API。我遵循了本教程https://cloud.google.com/translate/docs/quickstart-client-libraries?authuser=2#client-libraries-install-php
但是当我尝试运行代码进行翻译时,出现此错误-
Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/. To disable this warning, set SUPPRESS_GCLOUD_CREDS_WARNING environment variable to "true".
这是我的代码:
public static function gcloud(){
# Your Google Cloud Platform project ID
$projectId = 'mybot';
# Instantiates a client
$translate = new TranslateClient([
'projectId' => $projectId
]);
# The text to translate
$text = 'Hello, world!';
# The target language
$target = 'ru';
# Translates some text into Russian
$translation = $translate->translate($text, [
'target' => $target
]);
echo 'Text: ' . $text . '
Translation: ' . $translation['text'];
}
我不知道可能是什么问题。
答案 0 :(得分:1)
您设置客户端库的凭据很可能使用datePicker
上的gcloud
凭据。这些是End User Credentials,与特定用户YOU绑定。客户端库需要Service Account Credentials,该{{3}}不受特定用户的约束。
通过转到 API和服务> 凭据并选择创建凭据> 服务帐户密钥来创建服务帐户凭据>。创建一个新的服务帐户,然后为您分配角色 Cloud Translation API管理员。这将下载包含以下字段的JSON文件:
~/.config/gcloud/application_default_credentials.json
现在将{
"type": "service_account",
"project_id": "YOUR_PROJECT_ID",
"private_key_id": "...",
"private_key": "...",
"client_email": "...",
"client_id": "...",
"auth_uri": "...",
"token_uri": "...",
"auth_provider_x509_cert_url": "...",
"client_x509_cert_url": "..."
}
环境变量设置为此文件的路径。注意“类型”字段是“ service_account”。在引发错误的凭据中,“类型”字段为“ authorized_user”。