Skip to content

Commit

Permalink
PubSub: Document how to choose the PubSub auth method (#8429)
Browse files Browse the repository at this point in the history
* Document how to choose the PubSub auth method

* Give more exposure to google-auth in PubSub docs

The link to the library is moved to the first paragraph of the
Authentication section for better visibility.

* Use publisher audience for the publisher client
  • Loading branch information
plamut authored and sduskis committed Jun 24, 2019
1 parent 20f83b1 commit ad405a7
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pubsub/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,36 @@ It is also possible to pull messages in a synchronous (blocking) fashion. To
learn more about subscribing, consult the `subscriber documentation`_.

.. _subscriber documentation: https://googleapis.github.io/google-cloud-python/latest/pubsub/subscriber/index.html


Authentication
^^^^^^^^^^^^^^

It is possible to specify the authentication method to use with the Pub/Sub
clients. This can be done by providing an explicit `Credentials`_ instance. Support
for various authentication methods is available from the `google-auth`_ library.

For example, to use JSON Web Tokens, provide a `google.auth.jwt.Credentials`_ instance:

.. code-block:: python
import json
from google.auth import jwt
service_account_info = json.load(open("service-account-info.json"))
audience = "https://pubsub.googleapis.com/google.pubsub.v1.Subscriber"
credentials = jwt.Credentials.from_service_account_info(
service_account_info, audience=audience
)
subscriber = pubsub_v1.SubscriberClient(credentials=credentials)
# The same for the publisher, except that the "audience" claim needs to be adjusted
publisher_audience = "https://pubsub.googleapis.com/google.pubsub.v1.Publisher"
credentials_pub = credentials.with_claims(audience=publisher_audience)
publisher = pubsub_v1.PublisherClient(credentials=credentials_pub)
.. _Credentials: https://google-auth.readthedocs.io/en/latest/reference/google.auth.credentials.html#google.auth.credentials.Credentials
.. _google-auth: https://google-auth.readthedocs.io/en/latest/index.html
.. _google.auth.jwt.Credentials: https://google-auth.readthedocs.io/en/latest/reference/google.auth.jwt.html#google.auth.jwt.Credentials

0 comments on commit ad405a7

Please sign in to comment.