5

मैं निम्नलिखित कोड, Google's documentation से संशोधित किया है:Google मेघ API - एप्लिकेशन डिफ़ॉल्ट साख

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

अभी तक यह त्रुटि देता है जब मैं इसे चलाने:

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' 

मैं विभिन्न विन्यास की कोशिश की है, उदाहरण के लिए फ़ाइल के पथ को बदलना। जैसा कि आप देखते हैं, मैंने वैरिएबल के तीन अलग-अलग रूप भी किए हैं जिन्हें मैं तुरंत सोच सकता हूं (दो पर्यावरण, एक नहीं)।

मुझे पूरा यकीन नहीं है कि आगे कहां देखना है। क्या मुझे पर्यावरण चर सेट करने के विभिन्न तरीकों को देखना चाहिए या क्या मुझे पथ को अलग तरीके से परिभाषित करना चाहिए? ऐसा करने के सही तरीके क्या हैं? क्या त्रुटि के लिए कोई अन्य कारण है?

+0

लिया कहाँ से कोड चल रहे हैं (उदाहरण के लिए। App इंजन prod या देव, कंप्यूट इंजन, स्थानीय स्तर पर)? विंडोज़/अपाचे सिस्टम पर – Adam

+0

@ एडम स्थानीय रूप से। हालांकि, मैं कुछ ऐसा करने के लिए निर्माण कर रहा हूं जो लिनक्स/एनजीआईएनएक्स सिस्टम पर चलेगा। – laef

उत्तर

13

आपके द्वारा उपयोग की जाने वाली किसी भी विधि का उपयोग करने की कोशिश करने के बजाय आपको 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

कृपया पढ़ें "[मैं एक अच्छा जवाब कैसे लिखूं] (http://stackoverflow.com/help/how-to-answer)" – fusion3k

+0

@ fusion3k मुझे अपने उत्तर के साथ कोई समस्या नहीं दिखाई दे रही है। ठीक है, यह छोटा है, लेकिन उन्होंने पूछा है, "क्या मुझे पथ को अलग तरीके से परिभाषित करना चाहिए?" और मेरा जवाब दिखाता है कि मार्ग को अलग तरीके से कैसे परिभाषित किया जाए, इस प्रकार सवाल का जवाब दिया जाए। मेरा मानना ​​है कि मेरा जवाब आत्म व्याख्यात्मक है और किसी और स्पष्टीकरण की आवश्यकता नहीं है। "ब्रेवटी स्वीकार्य है"। मुझे यह सुनकर खुशी हो रही है कि आप कैसे सोचते हैं कि मैं जवाब में सुधार कर सकता हूं? –

+0

सबसे पहले: आपके उत्तर कई उपयोगकर्ताओं द्वारा 'अस्पष्ट' के रूप में रिपोर्ट किया गया है, इसलिए कोई भी सोचता है कि यह 'आत्म व्याख्यात्मक' नहीं है। मैं आपसे सहमत हो सकता हूं: मेरे लिए - और शायद ओपी के लिए - उत्तर आत्म-व्याख्यात्मक है, लेकिन यह साइट चैट-जैसी नहीं है, प्रश्न और उत्तर भविष्य के आगंतुकों के लिए भी मौजूद हैं। एक बेहतर उत्तर ऐसा हो सकता है "पर्यावरण चर सेट करने की आपकी कोशिश मान्य नहीं है, आपको इसके बजाय इस वाक्यविन्यास का उपयोग करना होगा ... $ _ENV का अर्थ है कि ... अधिक जानकारी के लिए दस्तावेज़ देखें ... आदि"। कई तरीके हैं, लेकिन "दर्शक" कोड की एक पंक्ति और अधिक चाहते हैं। – fusion3k

0
I agree with above answer only want to describe if user getting error in php using 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']; 
?>