0

I am new to programming and am trying to import the existing GCP resources using python. But I am getting this particular "Default Credentials Error" at the starting of the code itself. Any help is appreciated.

Exception has occurred: DefaultCredentialsError
Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
  File "C:\Users\dilee\Desktop\Python\GCP\start.py", line 4, in <module>
    publisher = pubsub_v1.PublisherClient()

Second Error

Exception has occurred: DefaultCredentialsError
File "C:\Users\dilee\Desktop\PythonGCP\learning-project-311506-1b36ea6ccc86.json" was not found.
  File "C:\Users\dilee\Desktop\PythonGCP\start.py", line 6, in <module>
    publisher = pubsub.PublisherClient()
1
  • A good place to start, if you don't feel comfortable with auth strategies, would be here: Authentication overview, and then continue reading the topics on the left, depending on what authentication strategy you choose to use. Commented Apr 22, 2021 at 13:11

1 Answer 1

2

Google Cloud API needs actually a key to know which google account you are using and what are your rights. This key takes the form of credentials.

So basically, what you have to do is to authentificate yourself first. If you are coding locally, the best method is to type first gcloud auth application-default login (if you want to set your default login) or only gcloud auth login (but then only the currently used command line will be identificated to google cloud).

Then you'd be able to use GCP API normally :)

See Here if you need more details.

Sign up to request clarification or add additional context in comments.

3 Comments

Now it's showing a different error that the JSON file cannot be found although I set the path correctly. Exception has occurred: DefaultCredentialsError File "C:\Users\dilee\Desktop\PythonGCP\learning-project-311506-1b36ea6ccc86.json" was not found. File "C:\Users\dilee\Desktop\PythonGCP\start.py", line 6, in <module> publisher = pubsub.PublisherClient()
I am very unsure why it cannot find the file from the path you gave it, even though you gave it the right one. You can though try to replace double quotes by single quotes. As temporary solutions though, I think that you can try to set the environment variables directly on your python file. You only have to wite: import os and then os.environ["GOOGLE_APPLICATION_CREDENTIALS"] ="path_to_json.json" Be careful though to write it correctly with / instead (I think it works under Windows) or with \\ instead for a simple \ as it would be mistaken for an escaped character.
Actually, the same path worker after I restarted the visual studio code. Thank you anyway for the alternate solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.