pythongoogle-cloud-nl

Can't set project in Cloud Natural Language API


I'm using the api sample as found here.

Running gcloud config list shows my default project as project = multichoice-insights

Important extract of the code shows that I get the default credentials:

  credentials = GoogleCredentials.get_application_default().create_scoped(
  ['https://www.googleapis.com/auth/cloud-platform'],
)

However, when I try and use the api, I get the error message:

googleapiclient.errors.HttpError: <HttpError 403 when requesting 
https://language.googleapis.com/v1beta1/documents:analyzeSentiment?
alt=json returned "Google Cloud Natural Language API has not been used 
in project google.com:cloudsdktool before or it is disabled. Enable it 
by visiting
   https://console.developers.google.com/apis/api/language.googleapis.com/over
view?project=google.com:cloudsdktool then retry. If you enabled this 
API recently, wait a few minutes for the action to propagate to our 
systems and retry.">

I think this shows that the project used: project=google.com:cloudsdktool

How can I change this to project-name?

I have enabled this api in the console.


Solution

  • For anyone that has this issue:

    One needs to set their Environment variable. If you are using virtualenvs, set the variable as follows.

    In the file: .virtualenvs/env_name/bin edit the file activate to contain the following line:

    GOOGLE_APPLICATION_CREDENTIALS=/abs/path/to/credentials.json
    

    This credentials.json is donwloaded from the api page. Full instructions can be gathered from here

    Hope this helps someone.