我正在尝试安装Google翻译脚本,但没有工作,经过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;
而错误是
致命错误:带有消息{“错误”:{“代码”:403,“消息”:“请求缺少有效的API键”的未命名异常'Google\Cloud\Core\Exception\ServiceException‘。“错误”:{“消息”:“请求缺少有效的API键。”、“域”:“全局”、“原因”:“禁用”},/var/www/vhosts/domain/httpdocs/dev/translator/php-docs-samples/translate/vendor/google/cloud-core/RequestWrapper.php:265堆栈跟踪中的“状态”:"PERMISSION_DENIED“}}:#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),(数组) #2 /var/www/vhosts/domain/httpdocs/dev/translator/php-d ( /var/www/vhosts/domain/httpdocs/dev/translator/php-docs-samples/translate/vendor/google/cloud-core/RequestWrapper.php中的第265号行)
我正确地设置了Google证书,我认为,我用的是油灰。
导出GOOGLE_APPLICATION_CREDENTIALS="/var/www/vhosts/domain.es/httpdocs/dev/translator/php-docs-samples/translate/keysss.json“
请帮帮我!
谢谢
发布于 2018-08-21 20:34:51
当应用程序由于文件丢失、凭据路径无效、环境变量分配不正确等原因而无法正确验证时,通常会引发此错误消息。请记住,在会话中设置环境变量值时,每次删除会话时都会重置该值。
基于此,我建议您验证凭证文件和文件路径是否被正确分配,并遵循手动获取和提供服务帐户凭据指南,以便将服务帐户文件直接指定到代码中;这样,您将能够永久地设置它,并验证是否正确传递服务凭据。
在代码示例中传递到服务帐户键的路径:
namespace Google\Cloud\Samples\Auth;
// Imports the Google Cloud Storage client library.
use Google\Cloud\Storage\StorageClient;
function auth_cloud_explicit($projectId, $serviceAccountPath)
{
# Explicitly use service account credentials by specifying the private key
# file.
$config = [
'keyFilePath' => $serviceAccountPath,
'projectId' => $projectId,
];
$storage = new StorageClient($config);
# Make an authenticated API request (listing storage buckets)
foreach ($storage->buckets() as $bucket) {
printf('Bucket: %s' . PHP_EOL, $bucket->name());
}
}
https://stackoverflow.com/questions/51954793
复制相似问题