Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Analyzing the image's text with the Natural Language API

HI 

 i try many times to run this code but some thing wrong , 

  1. To set up the API request, create a nl-request.json file with the following:

{
  "document":{
    "type":"PLAIN_TEXT",
    "content":"your_text_here"
  },
  "encodingType":"UTF8"
}
Copied!
content_copy

In the request, you're telling the Natural Language API about the text you're sending:

  • type: supported type values are PLAIN_TEXT or HTML.

  • content: pass the text to send to the Natural Language API for analysis. The Natural Language API also supports sending files stored in Cloud Storage for text processing. To send a file from Cloud Storage, replace content with gcsContentUri and use the value of the text file's uri in Cloud Storage.

  • encodingType: tells the API which type of text encoding to use when processing the text. The API will use this to calculate where specific entities appear in the text.

  1. Run this Bash command in Cloud Shell to copy the translated text into the content block of the Natural Language API request:

STR=$(jq .data.translations[0].translatedText  translation-response.json) && STR="${STR//\"}" && sed -i "s|your_text_here|$STR|g" nl-request.json
Copied!
content_copy

The nl-request.json file now contains the translated English text from the original image. Time to analyze it!

  1. Call the analyzeEntities endpoint of the Natural Language API with this curl request:

curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" \
  -s -X POST -H "Content-Type: application/json" --data-binary @nl-request.json

i have this issus

student_00_fc5405542deb@cloudshell:~ (qwiklabs-gcp-01-39d2648c29b0)$ curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" \
-s -X POST -H "Content-Type: application/json" --data-binary @nl-request.json
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"status": "PERMISSION_DENIED"
}
}

 

help plz 

 

Solved Solved
0 2 529
1 ACCEPTED SOLUTION

Good day @IBRAHIM-K,

Welcome to Google Cloud Community!

You are encountering this error since your API_KEY variable is invalid or missing. You can try checking your variable if it contains your API_KEY by running the following:

printenv API_KEY

If it does not contain your API_KEY, it is possible you were not able to export your API keys as a variable but if your variable API_KEY was able to produce an output that contains an API key, it is possible that your API key is invalid. To solve this problem, here is a step by step process on how to create an API KEY and export it as a variable. 

1. In your console > Click Navigation Menu > Select API & Services > Select Credentials
2. Click Create Credentials > Select API key (This will generate an API key) > Copy the API key that was generated
3. In your Cloud Shell, run the following: 

export API_KEY = <PASTE YOUR COPIED API KEY HERE>

Please note that you need to remove the symbols and >

You can try checking again if you're able to export the variable API_KEY by running the following:

printenv API_KEY​

It should output your API keys, after that you should be able to call the analyzeEntities endpoint.

Hope this is useful!

 

View solution in original post

2 REPLIES 2

Good day @IBRAHIM-K,

Welcome to Google Cloud Community!

You are encountering this error since your API_KEY variable is invalid or missing. You can try checking your variable if it contains your API_KEY by running the following:

printenv API_KEY

If it does not contain your API_KEY, it is possible you were not able to export your API keys as a variable but if your variable API_KEY was able to produce an output that contains an API key, it is possible that your API key is invalid. To solve this problem, here is a step by step process on how to create an API KEY and export it as a variable. 

1. In your console > Click Navigation Menu > Select API & Services > Select Credentials
2. Click Create Credentials > Select API key (This will generate an API key) > Copy the API key that was generated
3. In your Cloud Shell, run the following: 

export API_KEY = <PASTE YOUR COPIED API KEY HERE>

Please note that you need to remove the symbols and >

You can try checking again if you're able to export the variable API_KEY by running the following:

printenv API_KEY​

It should output your API keys, after that you should be able to call the analyzeEntities endpoint.

Hope this is useful!

 

thank you actually you are correct i do this 🙂