tree: 8b5d3df6c294e79a04f6096c8086862c6ee84928 [path history] [tgz]
  1. src/
  2. BUILD.bazel
  3. build.gradle
  4. pom.xml
  5. README.md
  6. settings.gradle
examples/example-gauth/README.md

Google Authentication Example

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.

  1. 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.

  2. Use an existing project, or create a project, say Google Auth Pubsub example. Note down the project ID of this project - say xyz123 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.

  1. Unless already enabled, enable the Cloud Pub/Sub API for your project by clicking Enable.

  2. Go to the GCP Pub/Sub console. Create a couple of new topics using the “+ CREATE TOPIC” button, say Topic1 and Topic2. You can also use the gcloud command to create a topic:

gcloud pubsub topics create Topic1
  1. You will now need to set up authentication and a service account in order to access Pub/Sub via gRPC APIs as described here. Assign the role Project -> Owner and for Key type select JSON. Once you click Create, 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. You can also use the gcloud shell commands to create the service account and the JSON file.

To build the examples

  1. Install gRPC Java library SNAPSHOT locally, including code generation plugin (Only need this step for non-released versions, e.g. master HEAD).

  2. Run in this directory:

$ ../gradlew installDist

How to run the example:

google-auth-client requires two command line arguments for the location of the JSON file and the project ID:

USAGE: GoogleAuthClient <path-to-JSON-file> <project-ID>

The first argument is the location of the JSON file you created in step 5 above. The second 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 /path/to/JSON/file 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"
]

Maven

If you prefer to use Maven:

  1. Install gRPC Java library SNAPSHOT locally, including code generation plugin (Only need this step for non-released versions, e.g. master HEAD).

  2. Run in this directory:

$ mvn verify
$ # Run the client
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.googleAuth.GoogleAuthClient -Dexec.args="/path/to/JSON/file projects/xyz123"

Bazel

If you prefer to use Bazel:

$ bazel build :google-auth-client
$ # Run the client
$ ../bazel-bin/google-auth-client /path/to/JSON/file projects/xyz123