0

我正在尝试为脚本设置 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"

请帮我!

谢谢

4

1 回答 1

2

当应用程序由于缺少文件、无效的凭据路径、不正确的环境变量分配等原因而未正确验证身份时,通常会引发此错误消息。请记住,当您在会话中设置环境变量值时,每次删除会话时都会重置它。

基于此,我建议您验证凭据文件和文件路径是否已正确分配,并遵循手动获取和提供服务帐户凭据指南,以便将您的服务帐户文件直接明确指定到您的代码中;通过这种方式,您将能够永久设置它并验证您是否正确传递了服务凭据。

在代码示例中将路径传递给服务帐户密钥:

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());
    }
}
于 2018-08-21T20:34:51.637 回答