Search code examples
google-cloud-platformgoogle-colaboratorygoogle-cloud-pubsub

Authenticating to pubsub in a co-lab notebook via the gcloud auth command


I would like to authenticate to pubsub via a co-lab notebook by using the !gcloud auth command. However, when I run that command, I am able to authenticate to cloud sql as well as to the gcp buckets, but I get the following error message when I run the publisher = pubsub.PublisherClient() command:

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

I want to avoid uploading my cloud credentials to a google drive folder to authenticate with pubsub, since i want to be able to share this notebook across my organization so that other users with the correct access rights can also run the notebook directly from their end without needing to upload their own service account credentials. Is there a way that I can do this? Thanks in advance.


Solution

  • Use:

    from google.colab import auth
    auth.authenticate_user()
    

    As in this example showing Google Cloud Storage access: https://colab.research.google.com/notebooks/io.ipynb#scrollTo=NQX0hbfYaEKc

    enter image description here