致命错误:未捕获的异常'Google \ Cloud \ Core \ Exception \ ServiceException'错误403

时间:2018-08-21 18:37:42

标签: api google-cloud-platform google-translate google-cloud-iam

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

错误是

  

致命错误:消息为“ {{错误”:{“代码”:403,“消息”:“请求缺少有效的API密钥”的未捕获异常'Google \ Cloud \ Core \ Exception \ ServiceException'。 “错误”:[{“消息”:“请求缺少有效的API密钥。”,“域”:“全局”,“原因”:“禁止”}],“状态”:“ PERMISSION_DENIED”}}'在/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 (对象(GuzzleHttp \ Psr7 \ Request),数组)#2 / var / www / vhosts / domain / httpdocs / dev / translator / php-d在/ var / www / vhosts / domain / httpdocs / dev / translat中或第265行的/php-docs-samples/translate/vendor/google/cloud-core/RequestWrapper.php

我正确设置了Google凭据,或者我认为我用过的腻子

  

导出   GOOGLE_APPLICATION_CREDENTIALS =“ / var / www / vhosts / domain.es / httpdocs / dev / translator / php-docs-samples / translate / keysss.json”

请帮助我!

谢谢

1 个答案:

答案 0 :(得分:0)

通常由于多种原因(例如文件丢失,凭据路径无效,环境变量分配不正确)而未正确地对应用程序进行认证时,会引发此错误消息。请记住,在会话中设置环境变量值时,每次删除会话时都会重置该值。

基于此,我建议您验证是否正确分配了凭证文件和文件路径,并遵循Obtaining and providing service account credentials manually指南,以便在代码中直接直接指定服务帐户文件;这样,您将能够对其进行永久设置,并验证您是否正确传递了服务凭据。

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

string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");