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

时间:2016-02-15 20:48:37

标签: php google-api google-cloud-platform google-api-php-client

我有以下代码,已从Google's documentation修改:

--interface tun0

然而,当我运行它时会返回错误:

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

我尝试了各种配置,例如更改文件的路径。如你所见,我还做了三种不同形式的变量,我可以立即想到(两个环境,一个不是)。

我不太确定下一步该去哪儿。我应该研究设置环境变量的不同方法,还是应该以不同的方式定义路径?这样做的正确方法是什么?该错误还有其他原因吗?

5 个答案:

答案 0 :(得分:28)

您需要使用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();

答案 1 :(得分:1)

我同意上述答案,但只想描述用户是否在使用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'];
?>

答案 2 :(得分:0)

使用这个对我有用的

applicationContext.xml

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

尝试使用如下所示的ImageAnnotator 运行代码时,请注意您会得到一个错误: 无法构造ApplicationDefaultCredentials

解决方案

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

但不起作用。 操作系统: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);
        }