13

I am trying to use the google speech API for converting an audio file to text, but facing some problems with the documentation.I have enabled the speech API and created an service account file. I also got the service account credentials activated. Then I wrote the following command to retrieve the authorization token using my service account.

gcloud auth application-default print-access-token

But it is returning this message enter image description here

I don't understand why this is happening because I already have an VM instance created in my Compute Engine. Can anybody explain where am I going wrong with this? Also, is there any other way to authenticate my account and call the google speech API?

JSnow
  • 929
  • 2
  • 11
  • 24
  • 3
    Sidenote: you may want to copy/paste error text instead of using screenshots; that way the text can be indexed by search engines and read by blind people and stuff – Félix Paradis Oct 05 '18 at 18:29

3 Answers3

37

gcloud credentials and and application default credentials are managed separately. If you activated service account key file this only can be used for gcloud commands but not for application default credentials.

gcloud auth application-default set of commands are there only to manage application default and having nothing to do with commands in gcloud auth.

There a few ways to "activate" application default credentials:

  • Use your user account
    • Run gcloud auth application-default login, or
  • Use service account (preferred)

    • set environment variable GOOGLE_APPLICATION_CREDENTIALS=path/to/your/service_accont_key_file.json
    • copy path/to/your/service_accont_key_file.json to ~/.config/gcloud/application_default_credentials.json`

      • On windows this might be different, run

        gcloud info --format="value(config.paths.global_config_dir)"
        

        to get a path to your config directory.

now command like

gcloud auth application-default print-access-token

should work.

asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
cherba
  • 8,681
  • 3
  • 27
  • 34
  • 2
    I try to use the service account key.json method to authenticate my localhost to communicate with the Google api service. I download the json then go to the Environment path section of the Window and set the GOOGLE_APPLICATION_CREDENTIALS but still not able to access the google api from local. – Prabhat Yadav Sep 19 '17 at 11:38
  • I presume you work on Windows. Just to confirm, once you set your environment variable you need to also restart any application where you trying to use it. Try to print value of environment variable to confirm that indeed it propagated. If it still not working, you might want to post a question on stack overflow and be specific on how your trying to access, give example or code snippet so others can replicate. – cherba Sep 20 '17 at 14:56
  • 1
    This is what really worked for me: `mkdir ~/.config/gcloud` then `mv ~/.config/gcloud/keyfile.json ~/.config/gcloud/application_default_credentials.json` – zdeeb Nov 10 '17 at 01:36
3

As A solution you can Download the GCLOUD console and then RUN this command on console "gcloud auth application-default" login with the gmail account which have the project access and then you will get access of google api which are enabled.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Prabhat Yadav
  • 1,181
  • 6
  • 18
  • 29
  • I've removed the question from your answer so only the solution is left. You should use the [Ask Question](https://stackoverflow.com/questions/ask) button if you have a new question (even if it's a related question). – g00glen00b Sep 25 '17 at 07:26
1

you can generate your key json file on gcloud using this command: gcloud auth application-default login

google will display a link to confirm, once you do, there will be the json file ina temp folder displayed

enter image description here

Tiago Medici
  • 1,944
  • 22
  • 22