Skip to content

Commit 94a0a0d

Browse files
authoredOct 4, 2024··
example-gauth: Use application default creds instead of file argument (#11595)
Also removed unnecessary refreshAccessToken() and fixed the reference to README.md. Fixes #5677
1 parent 35f0d56 commit 94a0a0d

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed
 

‎examples/example-gauth/README.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ gcloud pubsub topics create Topic1
4343
5. You will now need to set up [authentication](https://cloud.google.com/docs/authentication/) and a
4444
[service account](https://cloud.google.com/docs/authentication/#service_accounts) in order to access
4545
Pub/Sub via gRPC APIs as described [here](https://cloud.google.com/iam/docs/creating-managing-service-accounts).
46-
Assign the [role](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts) `Project -> Owner`
46+
(**Note:** This step is unnecessary on Google platforms (Google App Engine / Google Cloud Shell / Google Compute Engine) as it will
47+
automatically use the in-built Google credentials). Assign the [role](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts) `Project -> Owner`
4748
and for Key type select JSON. Once you click `Create`, a JSON file containing your key is downloaded to
4849
your computer. Note down the path of this file or copy this file to the computer and file system where
4950
you 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)
52-
and [the JSON file](https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-create-gcloud).
51+
`/path/to/JSON/file` Set the value of the environment variable GOOGLE_APPLICATION_CREDENTIALS to this file path. You can also use the `gcloud` shell commands to
52+
[create the service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts#iam-service-accounts-create-gcloud).
5353

5454
#### To build the examples
5555

@@ -62,19 +62,18 @@ $ ../gradlew installDist
6262

6363

6464
#### 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:
65+
`google-auth-client` requires one command line argument for the project ID:
6666

6767
```text
68-
USAGE: GoogleAuthClient <path-to-JSON-file> <project-ID>
68+
USAGE: GoogleAuthClient <project-ID>
6969
```
7070

71-
The first argument <path-to-JSON-file> is the location of the JSON file you created in step 5 above.
72-
The second argument <project-ID> is the project ID in the form "projects/xyz123" where "xyz123" is
71+
The first argument <project-ID> is the project ID in the form "projects/xyz123" where "xyz123" is
7372
the project ID of the project you created (or used) in step 2 above.
7473

7574
```bash
7675
# Run the client
77-
./build/install/example-gauth/bin/google-auth-client /path/to/JSON/file projects/xyz123
76+
./build/install/example-gauth/bin/google-auth-client projects/xyz123
7877
```
7978
That's it! The client will show the list of Pub/Sub topics for the project as follows:
8079

@@ -93,13 +92,13 @@ the project ID of the project you created (or used) in step 2 above.
9392
```
9493
$ mvn verify
9594
$ # Run the client
96-
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.googleAuth.GoogleAuthClient -Dexec.args="/path/to/JSON/file projects/xyz123"
95+
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.googleAuth.GoogleAuthClient -Dexec.args="projects/xyz123"
9796
```
9897

9998
## Bazel
10099
If you prefer to use Bazel:
101100
```
102101
$ bazel build :google-auth-client
103102
$ # Run the client
104-
$ ../bazel-bin/google-auth-client /path/to/JSON/file projects/xyz123
103+
$ ../bazel-bin/google-auth-client projects/xyz123
105104
```

‎examples/example-gauth/src/main/java/io/grpc/examples/googleAuth/GoogleAuthClient.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
/**
3535
* Example to illustrate use of Google credentials as described in
36-
* @see <a href="../../../../../../GOOGLE_AUTH_EXAMPLE.md">Google Auth Example README</a>
36+
* @see <a href="../../../../../../README.md">Google Auth Example README</a>
3737
*
3838
* Also @see <a href="https://cloud.google.com/pubsub/docs/reference/rpc/">Google Cloud Pubsub via gRPC</a>
3939
*/
@@ -52,7 +52,7 @@ public class GoogleAuthClient {
5252
*
5353
* @param host host to connect to - typically "pubsub.googleapis.com"
5454
* @param port port to connect to - typically 443 - the TLS port
55-
* @param callCredentials the Google call credentials created from a JSON file
55+
* @param callCredentials the Google call credentials
5656
*/
5757
public GoogleAuthClient(String host, int port, CallCredentials callCredentials) {
5858
// Google API invocation requires a secure channel. Channels are secure by default (SSL/TLS)
@@ -63,7 +63,7 @@ public GoogleAuthClient(String host, int port, CallCredentials callCredentials)
6363
* Construct our gRPC client that connects to the pubsub server using an existing channel.
6464
*
6565
* @param channel channel that has been built already
66-
* @param callCredentials the Google call credentials created from a JSON file
66+
* @param callCredentials the Google call credentials
6767
*/
6868
GoogleAuthClient(ManagedChannel channel, CallCredentials callCredentials) {
6969
this.channel = channel;
@@ -101,32 +101,30 @@ public void getTopics(String projectID) {
101101

102102
/**
103103
* The app requires 2 arguments as described in
104-
* @see <a href="../../../../../../GOOGLE_AUTH_EXAMPLE.md">Google Auth Example README</a>
104+
* @see <a href="../../../../../../README.md">Google Auth Example README</a>
105105
*
106-
* arg0 = location of the JSON file for the service account you created in the GCP console
107-
* arg1 = project name in the form "projects/balmy-cirrus-225307" where "balmy-cirrus-225307" is
106+
* arg0 = project name in the form "projects/balmy-cirrus-225307" where "balmy-cirrus-225307" is
108107
* the project ID for the project you created.
109108
*
109+
* On non-Google platforms, the GOOGLE_APPLICATION_CREDENTIALS env variable should be set to the
110+
* location of the JSON file for the service account you created in the GCP console.
110111
*/
111112
public static void main(String[] args) throws Exception {
112-
if (args.length < 2) {
113-
logger.severe("Usage: please pass 2 arguments:\n" +
114-
"arg0 = location of the JSON file for the service account you created in the GCP console\n" +
115-
"arg1 = project name in the form \"projects/xyz\" where \"xyz\" is the project ID of the project you created.\n");
113+
if (args.length < 1) {
114+
logger.severe("Usage: please pass 1 argument:\n" +
115+
"arg0 = project name in the form \"projects/xyz\" where \"xyz\" is the project ID of the project you created.\n");
116116
System.exit(1);
117117
}
118-
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(args[0]));
118+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
119119

120120
// We need to create appropriate scope as per https://cloud.google.com/storage/docs/authentication#oauth-scopes
121121
credentials = credentials.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
122122

123-
// credentials must be refreshed before the access token is available
124-
credentials.refreshAccessToken();
125123
GoogleAuthClient client =
126124
new GoogleAuthClient("pubsub.googleapis.com", 443, MoreCallCredentials.from(credentials));
127125

128126
try {
129-
client.getTopics(args[1]);
127+
client.getTopics(args[0]);
130128
} finally {
131129
client.shutdown();
132130
}

0 commit comments

Comments
 (0)
Please sign in to comment.