Google Cloud vision API: "Request had insufficient authentication scopes."
Asked Answered
R

3

6

Hi I'm trying to use google's cloud vision API, specifically this example: https://cloud.google.com/vision/docs/label-tutorial#label_tutorial_1

I followed this tutorial: https://cloud.google.com/vision/docs/getting-started#set_up_a_service_account for setting up a service account but when I run my code I get:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://vision.googleapis.com/$discovery/rest?version=v1 returned "Request had insufficient authentication scopes.">

Can anybody help? I can't figure out what's going on, my code is 1:1 with the tutorials.

Rockingham answered 7/3, 2016 at 14:38 Comment(0)
M
7

You need to clean ~/.credentials/sheets.googleapis.com-python-quickstart.json from your computer. Then define your scope as https://www.googleapis.com/auth/drive. Run your code again, it should ask you to re-authorize. Then run your code.

Monopetalous answered 27/7, 2016 at 16:29 Comment(2)
I couldn't find this file on my computer. I tried unhiding folders and still couldn't find the directory (i'm using a Mac). Any help on where to find it?Desperado
You need to delete token.json from your computer's local folder. Then rerun your program and reauthenticate (a new browser webpage will open).Sawyers
K
3

I was getting the same error. I did the following:

sudo apt-get update & apt-get upgrade

pip install --upgrade google-api-python-client

then initialized the gcloud sdk via ...

gcloud init 

and voila it started working!

You can also try re-generating the credentials from the API manager

Knucklebone answered 17/3, 2016 at 11:29 Comment(0)
H
0

You might want to use the google.cloud client library for the Vision API instead.

To authenticate with the right scope, you'll need to generate a service account in the Cloud Console, and point to it from your code (or environment variables). See the Vision auth section for more info:

Get a service account from the credentials manager in the Cloud Console. Then point to your project and JSON credentials file in your environment:

$ export GOOGLE_CLOUD_PROJECT="your-project-id-here"
$ export GOOGLE_APPLICATION_CREDENTIALS="/path/to/keyfile.json"

The demo you were trying to run (labeling an image) becomes:

>>> from google.cloud import vision
>>> client = vision.Client()
>>> image = client.image(source_uri='gs://my-storage-bucket/image.jpg')
>>> labels = image.detect_labels(limit=3)
>>> labels[0].description
'automobile'
>>> labels[0].score
0.9863683

(Code snippet taken from the docs)

Halpern answered 3/7, 2017 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.