Understanding How to Configure Google API Key

Asked 1 years ago, Updated 1 years ago, 542 views

I wanted to use Google Cloud PHP Natural Language, so I added code from github to clone
https://github.com/googleapis/google-cloud-php-language

You have enabled and retrieved the API Key in advance.

The sample code in the github just now looks like this


use Google\Cloud\Language\LanguageClient;

$language = new LanguageClient();

// Analyze a presence.
$announcement=$language->annotateText('Greetings from Michigan!');

// Check the sentence.
if($announcement->sentiment()>0){
    echo "This is a positive message.\n";
}

// Detect entities.
$entities=$announcement->entitiesByType('LOCATION');

foreach($entities as$entity){
    echo$entity ['name']."\n";
}

// Parse the syntax.
$tokens=$notation->tokensByTag('NOUN');

foreach($token as $token){
    echo$token['text']['content']."\n";
}

There is no part here to set the API key.
In fact, I still get an error when I try to access it.

{"error":{"code":403, "message":"The request is missing a valid API key.","status":"PERMISSION_DENIED"}

See to authenticate using the API key and it says to add it at the end of the URL.
POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY

However, there is no item to set API_KEY in the above code.
Or you can use the x-goog-api-key header to pass the key...

How can I set the API key?
Thank you for your cooperation.

google-cloud google-api

2023-01-12 22:41

1 Answers

I received similar questions and answers below.
See also

If you know keyFilePath, why don't you try it?

$language=new LanguageClient([]
    'projectId' = > 'my-project-id',
    'keyFilePath' = > '/path/to/my/keyfile.json'
]);


2023-01-13 09:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.