1Google Authentication Example 2============================================== 3 4This example illustrates how to access [Google APIs](https://cloud.google.com/apis/docs/overview) via gRPC and 5open source [Google API libraries](https://github.com/googleapis) using 6[Google authentication](https://developers.google.com/identity/protocols/OAuth2), specifically the 7[GoogleCredentials](https://github.com/googleapis/google-auth-library-java/blob/master/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java) 8class. 9 10The example requires grpc-java to be pre-built. Using a release tag will download the relevant binaries 11from a maven repository. But if you need the latest SNAPSHOT binaries you will need to follow 12[COMPILING](../COMPILING.md) to build these. 13 14Please follow the [steps](./README.md#to-build-the-examples) to build the examples. The build creates 15the script `google-auth-client` in the `build/install/examples/bin/` directory which can be 16used to run this example. 17 18The example uses [Google PubSub gRPC API](https://cloud.google.com/pubsub/docs/reference/rpc/) to get a list 19of PubSub topics for a project. You will need to perform the following steps to get the example to work. 20Wherever possible, the required UI links or `gcloud` shell commands are mentioned. 21 221. Create or use an existing [Google Cloud](https://cloud.google.com) account. In your account, you may need 23to enable features to exercise this example and this may cost some money. 24 252. Use an existing project, or [create a project](https://pantheon.corp.google.com/projectcreate), 26say `Google Auth Pubsub example`. Note down the project ID of this project - say `xyz123` for this example. 27Use the project drop-down from the top or use the cloud shell command 28``` 29gcloud projects list 30``` 31to get the project ID. 32 333. Unless already enabled, [enable the Cloud Pub/Sub API for your project](https://console.developers.google.com/apis/api/pubsub.googleapis.com/overview) 34by clicking `Enable`. 35 364. Go to the [GCP Pub/Sub console](https://pantheon.corp.google.com/cloudpubsub). Create a couple of new 37topics using the "+ CREATE TOPIC" button, say `Topic1` and `Topic2`. You can also use the gcloud command 38to create a topic: 39``` 40gcloud pubsub topics create Topic1 41``` 42 435. You will now need to set up [authentication](https://cloud.google.com/docs/authentication/) and a 44[service account](https://cloud.google.com/docs/authentication/#service_accounts) in order to access 45Pub/Sub via gRPC APIs as described [here](https://cloud.google.com/iam/docs/creating-managing-service-accounts). 46Assign the [role](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts) `Project -> Owner` 47and for Key type select JSON. Once you click `Create`, a JSON file containing your key is downloaded to 48your computer. Note down the path of this file or copy this file to the computer and file system where 49you will be running the example application as described later. Assume this JSON file is available at 50`/path/to/JSON/file`. You can also use the `gcloud` shell commands to 51[create the service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts#iam-service-accounts-create-gcloud) 52and [the JSON file](https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-create-gcloud). 53 54#### To build the examples 55 561. **[Install gRPC Java library SNAPSHOT locally, including code generation plugin](../../COMPILING.md) (Only need this step for non-released versions, e.g. master HEAD).** 57 582. Run in this directory: 59``` 60$ ../gradlew installDist 61``` 62 63 64#### How to run the example: 65`google-auth-client` requires two command line arguments for the location of the JSON file and the project ID: 66 67 ```text 68USAGE: GoogleAuthClient <path-to-JSON-file> <project-ID> 69``` 70 71The first argument <path-to-JSON-file> is the location of the JSON file you created in step 5 above. 72The second argument <project-ID> is the project ID in the form "projects/xyz123" where "xyz123" is 73the project ID of the project you created (or used) in step 2 above. 74 75 ```bash 76# Run the client 77./build/install/example-gauth/bin/google-auth-client /path/to/JSON/file projects/xyz123 78``` 79 That's it! The client will show the list of Pub/Sub topics for the project as follows: 80 81 ``` 82 INFO: Topics list: 83 [name: "projects/xyz123/topics/Topic1" 84 , name: "projects/xyz123/topics/Topic2" 85 ] 86 ``` 87 88 ## Maven 89 If you prefer to use Maven: 90 1. **[Install gRPC Java library SNAPSHOT locally, including code generation plugin](../../COMPILING.md) (Only need this step for non-released versions, e.g. master HEAD).** 91 92 2. Run in this directory: 93 ``` 94 $ mvn verify 95 $ # Run the client 96 $ mvn exec:java -Dexec.mainClass=io.grpc.examples.googleAuth.GoogleAuthClient -Dexec.args="/path/to/JSON/file projects/xyz123" 97 ``` 98 99 ## Bazel 100 If you prefer to use Bazel: 101 ``` 102 $ bazel build :google-auth-client 103 $ # Run the client 104 $ ../bazel-bin/google-auth-client /path/to/JSON/file projects/xyz123 105 ```