Skip to content

Commit 4a4e87b

Browse files
committed
moved test into separate file
1 parent 067b1df commit 4a4e87b

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
lines changed

auth/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ You can then run a given `ClassName` via:
3535
mvn exec:java -Dexec.mainClass=com.google.cloud.auth.samples.AuthExample
3636
-Dexec.args="compute"
3737

38-
### Authenticating using an api key
38+
### Analyze text sentiment using LanguageService API with API key authentication
3939

40-
mvn exec:java -Dexec.mainClass=com.google.cloud.auth.samples.AuthExample
41-
-Dexec.args="apikey [api key]"
40+
Create an API key via the Google Cloud console: https://developers.google.com/workspace/guides/create-credentials#api-key
41+
42+
Once you have an API key you can run the command below replacing [api key] with your API key
43+
44+
mvn exec:java -Dexec.mainClass=com.google.cloud.auth.samples.ApiKeyAuthExample
45+
-Dexec.args=[api key]
4246

4347
## Downscoping with Credential Access Boundaries
4448

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2024 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.auth.samples;
18+
19+
// [START auth_cloud_api_key]
20+
import com.google.cloud.language.v2.AnalyzeSentimentResponse;
21+
import com.google.cloud.language.v2.Document;
22+
import com.google.cloud.language.v2.LanguageServiceClient;
23+
import com.google.cloud.language.v2.LanguageServiceSettings;
24+
import java.io.IOException;
25+
26+
// [END auth_cloud_api_key]
27+
28+
/**
29+
* Demonstrate how to authenticate requests using an API Key using the Language API as an example.
30+
*/
31+
public class ApiKeyAuthExample {
32+
33+
// [START auth_cloud_api_key]
34+
static void authApiKey(String apiKey) throws IOException {
35+
LanguageServiceSettings settings =
36+
LanguageServiceSettings.newBuilder().setApiKey(apiKey).build();
37+
LanguageServiceClient client = LanguageServiceClient.create(settings);
38+
Document document =
39+
Document.newBuilder().setContent("Hello World!").setType(Document.Type.PLAIN_TEXT).build();
40+
41+
AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document);
42+
43+
System.out.println(actualResponse.getDocumentSentiment().toString());
44+
45+
client.close();
46+
}
47+
// [END auth_cloud_api_keys]
48+
49+
public static void main(String[] args) throws IOException {
50+
if (args.length >= 1) {
51+
authApiKey(args[0]);
52+
return;
53+
} else {
54+
throw new IllegalArgumentException("Api key is required for this test");
55+
}
56+
}
57+
}

auth/src/main/java/com/google/cloud/auth/samples/AuthExample.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
import com.google.auth.appengine.AppEngineCredentials;
2121
import com.google.auth.oauth2.ComputeEngineCredentials;
2222
import com.google.auth.oauth2.GoogleCredentials;
23-
import com.google.cloud.language.v2.AnalyzeSentimentResponse;
24-
import com.google.cloud.language.v2.Document;
25-
import com.google.cloud.language.v2.LanguageServiceClient;
26-
import com.google.cloud.language.v2.LanguageServiceSettings;
2723
import com.google.cloud.storage.Bucket;
2824
import com.google.cloud.storage.Storage;
2925
import com.google.cloud.storage.StorageOptions;
@@ -32,7 +28,7 @@
3228
import java.io.IOException;
3329

3430
/**
35-
* Demonstrate various ways to authenticate requests using Cloud Storage/Language APIs as examples.
31+
* Demonstrate various ways to authenticate requests using Cloud Storage as an example call.
3632
*/
3733
public class AuthExample {
3834
// [START auth_cloud_implicit]
@@ -93,20 +89,6 @@ static void authAppEngineStandard() throws IOException {
9389
}
9490
// [END auth_cloud_explicit_app_engine]
9591

96-
// [START auth_cloud_api_key]
97-
static void authApiKey(String apiKey) throws IOException {
98-
LanguageServiceSettings settings =
99-
LanguageServiceSettings.newBuilder().setApiKey(apiKey).build();
100-
LanguageServiceClient client = LanguageServiceClient.create(settings);
101-
Document document =
102-
Document.newBuilder().setContent("Hello World!").setType(Document.Type.PLAIN_TEXT).build();
103-
104-
AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document);
105-
106-
System.out.println(actualResponse.getDocumentSentiment().toString());
107-
}
108-
// [END auth_cloud_api_keys]
109-
11092
public static void main(String[] args) throws IOException {
11193
if (args.length == 0) {
11294
authImplicit();
@@ -128,15 +110,6 @@ public static void main(String[] args) throws IOException {
128110
authAppEngineStandard();
129111
return;
130112
}
131-
if ("apikey".equals(args[0])) {
132-
if (args.length >= 2) {
133-
authApiKey(args[1]);
134-
} else {
135-
throw new IllegalArgumentException("Api key is required with 'apikey'.");
136-
}
137-
return;
138-
}
139-
140113
authImplicit();
141114
}
142115
}

0 commit comments

Comments
 (0)