Google Cloud API - default app credentials

I have the following code modified from google documentation :

$GOOGLE_APPLICATION_CREDENTIALS = "./[path].json"; $_ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "./[path].json"; $_SERVER["GOOGLE_APPLICATION_CREDENTIALS"] = "./[path].json"; $projectId = "[my project 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'); 

But when I run it, it returns an error:

 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' 

I tried various configurations, for example, changing the path to the file. As you can see, I also made three different forms of variables that I could immediately think of (two environments, not one).

I'm not quite sure where to look next. Should I look for different ways to set the environment variable or should I define the path differently? What are the right ways to do this? Is there another reason for the error?

+10
source share
4 answers

You need to use putenv() ( http://php.net/manual/en/function.putenv.php ) instead of trying to use any of the methods that you used ( $_ENV or $_SERVER ).

Taken from 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(); 
+28
source

I agree with the answer above, but want to describe only if the user gets an error in php using google nlp:

 <?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
source

Use it, it works for me

 # 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']; 
0
source

Alternatively, you can specify the path to your JSON file, like this

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

Source: https://habr.com/ru/post/1243086/


All Articles