6
votes

I am trying Google Cloud Vision API (beta) and it is returning "Permission Denied" message. But the "Cloud Vision API" is enabled for the project. Any help is appreciated.

Error Details from Google APIs Explorer

403 OK

- Show headers -

{
 "error": {
  "code": 403,
  "message": "Project has not activated the vision.googleapis.com API. Please enable the API for project google.com:apisexplorerconsole (#292824132082).",
  "status": "PERMISSION_DENIED",
  "details": [
   {
    "@type": "type.googleapis.com/google.rpc.Help",
    "links": [
     {
      "description": "Google developers console API activation",
      "url": "https://console.developers.google.com/project/292824132082/apiui/api"
     }
    ]
   }
  ]
 }
}
5

5 Answers

4
votes

You should start by using a CURL request instead of the API explorer or instead of your favorite programming language :

curl -v -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key=put_your_browser_secret_key_here --data-binary @put_your_req.json > result.json

{
"requests":[
{
  "image":{
    "content":"put_your_encoded_base64_image_content"
  },
  "features":[
    {
      "type":"LABEL_DETECTION",
      "maxResults":4
    }
  ]
}
]
}

as you may already know :

base64 your_image > your_encoded_base64_image_content

Hope that helps, I don't know why from API explorer there is a limitation.

1
votes
  1. Make sure you enable Cloud Vision API for your project. Note. It might be not listed by default. You can find it by search. enter image description here

  2. Create Service account key and download the key in JSON format. https://developers.google.com/identity/protocols/application-default-credentials#howtheywork

  3. Set GOOGLE_APPLICATION_CREDENTIALS environment variable.
    export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json

0
votes

For GOOGLE_APPLICATION_CREDENTIALS - please make sure you are pointing to the right credential file. You should ensure the environment variable points to the credentials file you download from the console. Otherwise it is going to use the gcloud project and will fail

0
votes

Create google vision api key environment variable:

GCV_API_KEY="YOUR_GOOGLE_VISION_API_KEY"

Create base64 image variable:

BASE64_IMAGE=$( base64 image.jpg )

Create json file to query google vision image label detection:

echo "{\"requests\":[{\"image\":{\"content\":\"$BASE64_IMAGE\"},\"features\":[{\"type\":\"LABEL_DETECTION\",\"maxResults\":4}]}]}" > gcv_label_request.json

Create json file to query google vision image text detection:

echo "{\"requests\":[{\"image\":{\"content\":\"$BASE64_IMAGE\"},\"features\":[{\"type\":\"TEXT_DETECTION\",\"maxResults\":4}]}]}" > gcv_ocr_request.json

Query google vision api to get image labels using curl and store repose in json file:

curl -v -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key=$GCV_API_KEY --data-binary @gcv_label_request.json > gcv_label_response.json

Query google vision api to get image text using curl and store repose in json file:

curl -v -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key=$GCV_API_KEY --data-binary @gcv_ocr_request.json > gcv_ocr_response.json
0
votes

You can go to https://console.developers.google.com/iam-admin/projects and make your project.

After that just enable the service which you want. Just make sure you save the key to safe location. This is simple step which can help you to enable the service.

In addition to make your project authenticate and authorize to vision api just follow following step.

https://googlecloudplatform.github.io/google-cloud-python/stable/google-cloud-auth.html