From 5a38bf224c476a9b3bf319875996684fc9c405a1 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Mon, 22 Jul 2024 15:37:57 -0400 Subject: [PATCH 1/2] docs: add sample code for authenticating with api keys --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index 746e18a4e5a3..309ad3ec6420 100644 --- a/README.md +++ b/README.md @@ -355,6 +355,44 @@ Credentials in the following locations (in order): 4. Google Cloud Shell built-in credentials 5. Google Compute Engine built-in credentials +### Authenticating with an API Key + +[Authenticating with API Keys](https://cloud.google.com/docs/authentication/api-keys) is supported by a handful of Google Cloud APIs, and the majority of Maps APIs. + +We are actively exploring ways to improve the API Key experience. +Currently, to use an API Key with a Java client library, you need to set the header for the relevant service Client manually. + +For example, to set the API Key with the [Language service](https://cloud.google.com/java/docs/reference/google-cloud-language/latest/overview): + +```java +public LanguageServiceClient createGrpcClientWithApiKey(String apiKey) throws Exception { + // Manually set the api key via the header + Map header = new HashMap() { {put("x-goog-api-key", apiKey);}}; + FixedHeaderProvider headerProvider = FixedHeaderProvider.create(header); + + // Create the client + TransportChannelProvider transportChannelProvider = InstantiatingGrpcChannelProvider.newBuilder().setHeaderProvider(headerProvider).build(); + LanguageServiceSettings settings = LanguageServiceSettings.newBuilder().setTransportChannelProvider(transportChannelProvider).build(); + LanguageServiceClient client = LanguageServiceClient.create(settings); + return client; + } +``` + +An example instantiation with the Language Client using rest: +```java + public LanguageServiceClient createRestClientWithApiKey(String apiKey) throws Exception { + // Manually set the api key header + Map header = new HashMap() { {put("x-goog-api-key", apiKey);}}; + FixedHeaderProvider headerProvider = FixedHeaderProvider.create(header); + + // Create the client + TransportChannelProvider transportChannelProvider = InstantiatingHttpJsonChannelProvider.newBuilder().setHeaderProvider(headerProvider).build(); + LanguageServiceSettings settings = LanguageServiceSettings.newBuilder().setTransportChannelProvider(transportChannelProvider).build(); + LanguageServiceClient client = LanguageServiceClient.create(settings); + return client; + } +``` + ## Troubleshooting To get help, follow the instructions in the [Troubleshooting document](https://github.com/googleapis/google-cloud-java/blob/main/TROUBLESHOOTING.md). From b7bda34e97c029ebd825475d8128f56172a6d946 Mon Sep 17 00:00:00 2001 From: Alice <65933803+alicejli@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:35:36 -0400 Subject: [PATCH 2/2] Update README.md Co-authored-by: Blake Li --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 309ad3ec6420..275f2794c36e 100644 --- a/README.md +++ b/README.md @@ -357,7 +357,7 @@ Credentials in the following locations (in order): ### Authenticating with an API Key -[Authenticating with API Keys](https://cloud.google.com/docs/authentication/api-keys) is supported by a handful of Google Cloud APIs, and the majority of Maps APIs. +[Authenticating with API Keys](https://cloud.google.com/docs/authentication/api-keys) is supported by a handful of Google Cloud APIs. We are actively exploring ways to improve the API Key experience. Currently, to use an API Key with a Java client library, you need to set the header for the relevant service Client manually.