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