1

Could some one help me with sample application from this site: https://cloud.google.com/natural-language/docs/quickstart-client-libraries#client-libraries-install-php. I did, at least I think I did, everything what is required. And I am constantly getting error 403:

Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message '{ "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } } ' in C:\Users\Admin\Dropbox\HTDOCS\nlp\vendor\google\cloud\Core\src\RequestWrapper.php:263 Stack trace: #0 

C:\Users\Admin\Dropbox\HTDOCS\nlp\vendor\google\cloud\Core\src\RequestWrapper.php(168): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException)) #1 

C:\Users\Admin\Dropbox\HTDOCS\nlp\vendor\google\cloud\Core\src\RestTrait.php(96): Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array) #2 

C:\Users\Admin\Dropbox\HTDOCS\nlp\vendor\google\cloud\Language\src\Connection\Rest.php(69): Google\Cloud\Language\Connection\Rest->send('documents', 'analyzeSentimen...', Array) #3 

C:\Users\Admin\Dropbox\HTDOCS\nlp\vendor\google\cloud\Language\src\LanguageClient.php(221): Google\Cloud\Language\Connection\Rest->analyzeSentiment(Array) #4 

C:\Users in C:\Users\Admin\Dropbox\HTDOCS\nlp\vendor\google\cloud\Core\src\RequestWrapper.php on line 263 
Victor M Perez
  • 2,185
  • 3
  • 19
  • 22
Slit
  • 491
  • 1
  • 7
  • 20
  • The error you have is `{ "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }`. It seems you haven't provided a valid API KEY. See how to do it [here](https://cloud.google.com/docs/authentication/api-keys). Also try to follow the tutorial from the github repo where there are more detailed instructions https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language Tell me if this works please – Victor M Perez May 22 '18 at 11:01
  • 1
    Where to put API key in demo code that I gave in a link? A created env. variable with link to json file that I downloaded... I am working on local machine, WIN 10. – Slit May 22 '18 at 11:23
  • It seems getting the API key doesn't directly apply in this case. Then please follow the instructions here https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language . I ran it from the cloud shell as well as locally from my linux machine and it works. Tell me the results please :-) – Victor M Perez May 22 '18 at 11:53
  • @ Victor Herasme Perez. I installed Composer on my cloud. But I am getting error in console: "Cloud Natural Language API has not been used in project 618104708054 before or it is disabled. Enable it by visiting https://console.developers.google.com/ap is/api/language.googleapis.com/overview?project=618104708054 then retry – Slit May 24 '18 at 11:50
  • On that link there is a message: The API "language.googleapis.com" doesn't exist or you don't have permission to access it Tracking Number: 4107029527597204881 – Slit May 24 '18 at 12:14
  • 1
    You have to create a service account with permissions for this API. You can create an service account like in [here](https://stackoverflow.com/a/44192942/3058302) – Mangu May 28 '18 at 07:30
  • @Mangu It worked. Why it was so hard for Google to say this somewhere in manule. Note: command: export GOOGLE_APPLICATION_CREDENTIALS=key.json, didn't work. I had to take the key.json form Credentials option. – Slit May 28 '18 at 08:40
  • Well, the tutorial you were following says that you have to do that. I will post this as an answer, since it solved your issue. – Mangu May 28 '18 at 09:06
  • @Mangu and I did it but using GUI. And it didn't work. – Slit May 28 '18 at 09:07

1 Answers1

0

You have to create a service account with permissions for this API, and then download a private key as a JSON file. It is mentioned in the tutorial you were following, but you can also do that via the command line like in here.

  $ gcloud iam service-accounts create my-account
  $ gcloud iam service-accounts keys create key.json
    --iam-account=my-account@my-project.iam.gserviceaccount.com
  $ export GOOGLE_APPLICATION_CREDENTIALS=key.json
Mangu
  • 3,160
  • 2
  • 25
  • 42