我正在尝试为脚本设置 Google Cloud API Translate 但不起作用,经过 3 小时调查后,我向您寻求帮助
quickstart.php 的代码是
<?php
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use t
his file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
error_reporting(E_ALL);
ini_set('display_errors', '1');
# [START translate_quickstart]
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Translate\TranslateClient;
# Your Google Cloud Platform project ID
$projectId = 'directed-radius-214010';
# Instantiates a client
$translate = new TranslateClient([
'projectId' => $projectId
]);
# The text to translate
$text = 'Hello, world!';
# The target language
$target = 'fr';
# Translates some text into Russian
$translation = $translate->translate($text, [
'target' => $target
]);
echo 'Text: ' . $text . '
Translation: ' . $translation['text'];
# [END translate_quickstart]
return $translation;
错误是
致命错误:未捕获的异常 'Google\Cloud\Core\Exception\ServiceException' 带有消息 '{ "error": { "code": 403, "message": "The request is missing a valid API key.", "errors" : [ { "message": "请求缺少有效的 API 密钥。", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } } ' in /var /www/vhosts/domain/httpdocs/dev/translator/php-docs-samples/translate/vendor/google/cloud-core/RequestWrapper.php:265 堆栈跟踪:#0 /var/www/vhosts/domain/httpdocs/ dev/translator/php-docs-samples/translate/vendor/google/cloud-core/RequestWrapper.php(170): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException)) #1 /var/www/vhosts/domain/httpdocs/dev/translator/php-docs-samples/translate/vendor/google/cloud-core/RestTrait.php(96) : Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array) #2 /var/www/vhosts/domain/httpdocs/dev/translator/php-d in /var/www/vhosts /domain/httpdocs/dev/translator/php-docs-samples/translate/vendor/google/cloud-core/RequestWrapper.php 在第 265 行
我正确地设置了谷歌凭据,或者我认为,我用腻子
导出 GOOGLE_APPLICATION_CREDENTIALS="/var/www/vhosts/domain.es/httpdocs/dev/translator/php-docs-samples/translate/keysss.json"
请帮我!
谢谢