|
33 | 33 |
|
34 | 34 | /** |
35 | 35 | * 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> |
37 | 37 | * |
38 | 38 | * Also @see <a href="https://cloud.google.com/pubsub/docs/reference/rpc/">Google Cloud Pubsub via gRPC</a> |
39 | 39 | */ |
@@ -52,7 +52,7 @@ public class GoogleAuthClient { |
52 | 52 | * |
53 | 53 | * @param host host to connect to - typically "pubsub.googleapis.com" |
54 | 54 | * @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 |
56 | 56 | */ |
57 | 57 | public GoogleAuthClient(String host, int port, CallCredentials callCredentials) { |
58 | 58 | // 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) |
63 | 63 | * Construct our gRPC client that connects to the pubsub server using an existing channel. |
64 | 64 | * |
65 | 65 | * @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 |
67 | 67 | */ |
68 | 68 | GoogleAuthClient(ManagedChannel channel, CallCredentials callCredentials) { |
69 | 69 | this.channel = channel; |
@@ -101,32 +101,30 @@ public void getTopics(String projectID) { |
101 | 101 |
|
102 | 102 | /** |
103 | 103 | * 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> |
105 | 105 | * |
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 |
108 | 107 | * the project ID for the project you created. |
109 | 108 | * |
| 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. |
110 | 111 | */ |
111 | 112 | 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"); |
116 | 116 | System.exit(1); |
117 | 117 | } |
118 | | - GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(args[0])); |
| 118 | + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); |
119 | 119 |
|
120 | 120 | // We need to create appropriate scope as per https://cloud.google.com/storage/docs/authentication#oauth-scopes |
121 | 121 | credentials = credentials.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")); |
122 | 122 |
|
123 | | - // credentials must be refreshed before the access token is available |
124 | | - credentials.refreshAccessToken(); |
125 | 123 | GoogleAuthClient client = |
126 | 124 | new GoogleAuthClient("pubsub.googleapis.com", 443, MoreCallCredentials.from(credentials)); |
127 | 125 |
|
128 | 126 | try { |
129 | | - client.getTopics(args[1]); |
| 127 | + client.getTopics(args[0]); |
130 | 128 | } finally { |
131 | 129 | client.shutdown(); |
132 | 130 | } |
|