Php 谷歌云API-应用程序默认凭据
我有以下代码,修改自: 但当我运行它时,它返回错误:Php 谷歌云API-应用程序默认凭据,php,google-api,google-cloud-platform,google-api-php-client,Php,Google Api,Google Cloud Platform,Google Api Php Client,我有以下代码,修改自: 但当我运行它时,它返回错误: PHP Fatal error: Uncaught exception 'DomainException' with message 'Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information' 我尝试了各
PHP Fatal error: Uncaught exception 'DomainException' with message 'Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information'
我尝试了各种配置,例如更改文件的路径。如您所见,我还完成了我可以立即想到的三种不同形式的变量(两种环境,一种不是)
我不太确定下一步该去哪里看。我应该研究设置环境变量的不同方法,还是应该以不同的方式定义路径?正确的方法是什么?该错误还有其他原因吗?您需要使用
putenv()
(),而不是尝试使用您使用过的任何方法($\u ENV
或$\u SERVER
)
取自
我同意上面的回答,但只想描述一下,如果用户使用nlp google在php中出现错误:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Language\LanguageClient;
putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/sgupta/www/practise/nlp/google/cred.json'); //your path to file of cred
//$client->useApplicationDefaultCredentials();
# Your Google Cloud Platform project ID
$projectId = 'nlp-project-nname'; //your project name
# Instantiates a client
$language = new LanguageClient([
'projectId' => $projectId
]);
# The text to analyze
$text = 'Sachin Tendulkar';
# Detects the sentiment of the text
$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();
echo "<pre>";
print_r($annotation); die;
echo 'Text: ' . $text . '
Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
?>
用这个它对我有用
$client = new Google_Client();
$client->setAuthConfig('/path/to/credentials.json');
或者,您可以像这样定义json文件的路径
putenv("GOOGLE_APPLICATION_CREDENTIALS=/home/netwons/Downloads/AIz.json");
尝试使用如下所示的ImageAnnotator 运行代码时,请注意出现错误: 无法构造ApplicationDefaultCredentials 解决方案
$imageAnnotator = new ImageAnnotatorClient();
// $client->setAuthConfig('key.json');
# the name of the image file to annotate
$fileName = '2.png';
# prepare the image to be annotated
$image = file_get_contents($fileName);
# performs label detection on the image file
$response = $imageAnnotator->labelDetection($image);
$labels = $response->getLabelAnnotations();
if ($labels) {
echo("Labels:" . PHP_EOL);
foreach ($labels as $label) {
echo($label->getDescription() . PHP_EOL);
}
} else {
echo('No label found' . PHP_EOL);
}
但不起作用。
操作系统:Ubuntu
PHP版本:7.2.4
软件包名称和版本:google/cloud vision 0.19.1
这是代码
您在哪里运行代码(例如,应用程序引擎prod或dev、计算引擎,本地)?@Adam local,在Windows/Apache系统上。但是,我正在构建一个将在Linux/NGINX系统上运行的东西。请阅读“@fusion3k”,我的答案没有任何问题。好的,很短,但是他们问“我应该用不同的方式定义路径吗?”我的回答显示了如何用不同的方式定义路径,从而回答了这个问题。我相信我的答案是不言自明的&不需要任何进一步的解释。“简洁是可以接受的”。我很高兴听到你认为我可以如何改进答案?首先:你的答案被一些用户报告为“不清楚”,因此更多的人认为它不是“自解释的”。我可以同意你的看法:对于我来说——也许对于OP来说——答案是不言自明的,但这个网站不是聊天式的,问题和答案也为未来的访问者而存在。更好的答案可能是“您尝试设置的环境变量无效,您必须使用此语法而不是…$\u ENV意味着…请参阅文档以了解更多详细信息…等等”。有很多方法,但是“观众”想要更多的一行代码。@fusion3k好的,所有的要点都很公平&我已经更新了我的答案。不过,你第一次发布的链接并没有说这些,所以如果你打算否决投票,要求正确答案的人被删除,也许你可以先解释一下原因。谢谢你从这里得到了提示。但对于laravel项目,我最终将GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json添加到了.env文件中。只是在这里给别人一些帮助……你能进一步解释一下吗?哪些零件是必需的,为什么?
$client = new Google_Client();
$client->setAuthConfig('/path/to/credentials.json');
putenv("GOOGLE_APPLICATION_CREDENTIALS=/home/netwons/Downloads/AIz.json");
$imageAnnotator = new ImageAnnotatorClient();
// $client->setAuthConfig('key.json');
# the name of the image file to annotate
$fileName = '2.png';
# prepare the image to be annotated
$image = file_get_contents($fileName);
# performs label detection on the image file
$response = $imageAnnotator->labelDetection($image);
$labels = $response->getLabelAnnotations();
if ($labels) {
echo("Labels:" . PHP_EOL);
foreach ($labels as $label) {
echo($label->getDescription() . PHP_EOL);
}
} else {
echo('No label found' . PHP_EOL);
}