This example illustrates how to access Google APIs via gRPC and open source Google API libraries using Google authentication, specifically the GoogleCredentials class.
The example requires grpc-java to be pre-built. Using a release tag will download the relevant binaries from a maven repository. But if you need the latest SNAPSHOT binaries you will need to follow COMPILING to build these.
Please follow the steps to build the examples. The build creates
the script google-auth-client
in the build/install/examples/bin/
directory which can be
used to run this example.
The example uses Google PubSub gRPC API to get a list
of PubSub topics for a project. You will need to perform the following steps to get the example to work.
Wherever possible, the required UI links or gcloud
shell commands are mentioned.
-
Create or use an existing Google Cloud account. In your account, you may need to enable features to exercise this example and this may cost some money.
-
Use an existing project, or create a project, say
Google Auth Pubsub example
. Note down the project ID of this project - sayxyz123
for this example. Use the project drop-down from the top or use the cloud shell command
gcloud projects list
to get the project ID.
-
Unless already enabled, enable the Cloud Pub/Sub API for your project by clicking
Enable
. -
Go to the GCP Pub/Sub console. Create a couple of new topics using the "+ CREATE TOPIC" button, say
Topic1
andTopic2
. You can also use the gcloud command to create a topic:
gcloud pubsub topics create Topic1
- You will now need to set up authentication and a
service account in order to access
Pub/Sub via gRPC APIs as described here.
(Note: This step is unnecessary on Google platforms (Google App Engine / Google Cloud Shell / Google Compute Engine) as it will
automatically use the in-built Google credentials). Assign the role
Project -> Owner
and for Key type select JSON. Once you clickCreate
, a JSON file containing your key is downloaded to your computer. Note down the path of this file or copy this file to the computer and file system where you will be running the example application as described later. Assume this JSON file is available at/path/to/JSON/file
Set the value of the environment variable GOOGLE_APPLICATION_CREDENTIALS to this file path. You can also use thegcloud
shell commands to create the service account.
-
Install gRPC Java library SNAPSHOT locally, including code generation plugin (Only need this step for non-released versions, e.g. master HEAD).
-
Run in this directory:
$ ../gradlew installDist
google-auth-client
requires one command line argument for the project ID:
USAGE: GoogleAuthClient <project-ID>
The first argument is the project ID in the form "projects/xyz123" where "xyz123" is the project ID of the project you created (or used) in step 2 above.
# Run the client
./build/install/example-gauth/bin/google-auth-client projects/xyz123
That's it! The client will show the list of Pub/Sub topics for the project as follows:
INFO: Topics list:
[name: "projects/xyz123/topics/Topic1"
, name: "projects/xyz123/topics/Topic2"
]
If you prefer to use Maven:
-
Install gRPC Java library SNAPSHOT locally, including code generation plugin (Only need this step for non-released versions, e.g. master HEAD).
-
Run in this directory:
$ mvn verify
$ # Run the client
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.googleAuth.GoogleAuthClient -Dexec.args="projects/xyz123"
If you prefer to use Bazel:
$ bazel build :google-auth-client
$ # Run the client
$ ../bazel-bin/google-auth-client projects/xyz123