如何在 yii2 框架中将 Google Cloud Vision API 凭证密钥文件指向 GOOGLE_APPLICATION_CREDENTIALS 变量 [英] How to point Google Cloud Vision API credential key file to GOOGLE_APPLICATION_CREDENTIALS variable in yii2 framework

查看:54
本文介绍了如何在 yii2 框架中将 Google Cloud Vision API 凭证密钥文件指向 GOOGLE_APPLICATION_CREDENTIALS 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Google Cloud Vision API 进行图像识别,在我的 yii2 框架中一切都安装得很好.

I want to use Google Cloud Vision API for image recognition, Everything installed fine in my yii2 framework.

我收到身份验证错误,例如:

I'm getting authentication error like :

>  Google\Cloud\Core\Exception\ServiceException
>     {
>     "error": {
>     "code": 403,
>     "message": "The request is missing a valid API key.",
>     "status": "PERMISSION_DENIED"
>     }
>     }
>     1. in C:\xampp\htdocs\vofms\vendor\google\cloud-core\src\RequestWrapper.php
> at line 336
>     32732832933033133233333433533633733

如何在 yii2 框架中将我的 key.json 文件指向 GOOGLE_APPLICATION_CREDENTIALS 环境变量.

How to point my key.json file to GOOGLE_APPLICATION_CREDENTIALS environment variable In yii2 framework.

谢谢

推荐答案

当您查看 Cloud Vision 库时,您失败的重点是身份验证,而 Cloud Vision 是 Google Cloud PHP,其中您有 身份验证指南给出了以下建议.

The point you are failing is the Authentication as you are looking at the Cloud Vision library whereas the Cloud Vision is a part of the Google Cloud PHP in which you have the Authentication Guide given which recommends the following.

获得凭据文件后,它可用于创建经过身份验证的客户端.

Once you've obtained your credentials file, it may be used to create an authenticated client.

use Google\Cloud\Core\ServiceBuilder;

// Authenticate using a keyfile path
$cloud = new ServiceBuilder([
    'keyFilePath' => 'path/to/keyfile.json'
]);

// Authenticate using keyfile data
$cloud = new ServiceBuilder([
    'keyFile' => json_decode(file_get_contents('/path/to/keyfile.json'), true)
]);

如果您不希望在您的应用程序代码中嵌入您的身份验证信息,您也可以使用 应用默认凭据.

If you do not wish to embed your authentication information in your application code, you may also make use of Application Default Credentials.

use Google\Cloud\Core\ServiceBuilder;

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json');

$cloud = new ServiceBuilder();

GOOGLE_APPLICATION_CREDENTIALS 环境变量可能已在您的服务器配置中设置.

The GOOGLE_APPLICATION_CREDENTIALS environment variable may be set in your server configuration.

export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

例如:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"

这篇关于如何在 yii2 框架中将 Google Cloud Vision API 凭证密钥文件指向 GOOGLE_APPLICATION_CREDENTIALS 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆