首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >致命错误:非正常异常'Google\Cloud\Core\Exception\ServiceException‘错误403

致命错误:非正常异常'Google\Cloud\Core\Exception\ServiceException‘错误403
EN

Stack Overflow用户
提问于 2018-08-21 18:37:41
回答 1查看 5.9K关注 0票数 0

我正在尝试安装Google翻译脚本,但没有工作,经过3个小时的调查后,我向您寻求帮助

quickstart.php的代码是

代码语言:javascript
运行
复制
    <?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“

请帮帮我!

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-08-21 20:34:51

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

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

在代码示例中传递到服务帐户键的路径:

代码语言:javascript
运行
复制
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());
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51954793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档