Google 云 API - 应用程序默认凭据

Google Cloud API - Application Default Credentials

我有以下代码,修改自Google's documentation

        $GOOGLE_APPLICATION_CREDENTIALS = "./[path].json";
        $_ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "./[path].json";
        $_SERVER["GOOGLE_APPLICATION_CREDENTIALS"] = "./[path].json";

        $projectId = "[my project's ID']";
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->setScopes(['https://www.googleapis.com/auth/books']);
        $service = new Google_Service_Books($client);
        $results = $service->volumes->listVolumes('Henry David Thoreau');

然而当我 运行 它 returns 错误:

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() (http://php.net/manual/en/function.putenv.php) 而不是尝试使用任何您已经使用过的方法 ($_ENV$_SERVER)。

取自https://github.com/google/google-api-php-client/blob/master/UPGRADING.md#google_auth_assertioncredentials-has-been-removed

// OR use environment variables (recommended)

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client->useApplicationDefaultCredentials();

我同意上面的回答,但只想描述用户是否在 php 中使用 nlp google:

出错
<?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'];
?>

用这个对我有用

# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=../service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();
$accessToken = $token['access_token'];

或者,您可以像这样定义 json 文件的路径

$client = new Google_Client();
$client->setAuthConfig('/path/to/credentials.json');

尝试像下面这样使用 ImageAnnotator 当 运行 代码时,观察你得到一个错误: 无法构建 ApplicationDefaultCredentials

解决方案

putenv("GOOGLE_APPLICATION_CREDENTIALS=/home/netwons/Downloads/AIz.json");

但不起作用。 OS: Ubuntu PHP版本:7.2.4 包名和版本:google/cloud-vision 0.19.1 这是代码

$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);
        }