Skip to content

Commit e34c14d

Browse files
committed
feat: create java sample for authorizing in google cloud using api key credentials
1 parent 611b32b commit e34c14d

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

auth/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ 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
39+
40+
mvn exec:java -Dexec.mainClass=com.google.cloud.auth.samples.AuthExample
41+
-Dexec.args="apikey [api key]"
42+
3843
## Downscoping with Credential Access Boundaries
3944

4045
The same configuration above applies.
@@ -44,7 +49,7 @@ you must provide both a bucket name and object name under the TODO(developer): i
4449

4550
You can then run `DownscopingExample` via:
4651

47-
mvn exec:exec
52+
mvn exec:java -Dexec.mainClass=com.google.cloud.auth.samples.DownscopingExample
4853

4954
## Tests
5055
Run all tests:

auth/pom.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ limitations under the License.
4848
<dependency>
4949
<groupId>com.google.cloud</groupId>
5050
<artifactId>libraries-bom</artifactId>
51-
<version>26.32.0</version>
51+
<version>26.49.0</version>
5252
<type>pom</type>
5353
<scope>import</scope>
5454
</dependency>
@@ -72,6 +72,10 @@ limitations under the License.
7272
<groupId>com.google.cloud</groupId>
7373
<artifactId>google-cloud-apikeys</artifactId>
7474
</dependency>
75+
<dependency>
76+
<groupId>com.google.cloud</groupId>
77+
<artifactId>google-cloud-language</artifactId>
78+
</dependency>
7579
<!-- END dependencies -->
7680
<dependency>
7781
<groupId>junit</groupId>
@@ -104,11 +108,6 @@ limitations under the License.
104108
</executions>
105109
<configuration>
106110
<executable>java</executable>
107-
<arguments>
108-
<argument>-classpath</argument>
109-
<classpath />
110-
<argument>com.google.cloud.auth.samples.DownscopingExample</argument>
111-
</arguments>
112111
</configuration>
113112
</plugin>
114113
</plugins>

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
import com.google.common.collect.Lists;
2727
import java.io.FileInputStream;
2828
import java.io.IOException;
29+
import com.google.cloud.language.v2.AnalyzeSentimentResponse;
30+
import com.google.cloud.language.v2.Document;
31+
import com.google.cloud.language.v2.LanguageServiceClient;
32+
import com.google.cloud.language.v2.LanguageServiceSettings;
2933

3034
/**
3135
* Demonstrate various ways to authenticate requests using Cloud Storage as an example call.
@@ -89,6 +93,21 @@ static void authAppEngineStandard() throws IOException {
8993
}
9094
// [END auth_cloud_explicit_app_engine]
9195

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+
102+
Document document =
103+
Document.newBuilder().setContent("Hello World!").setType(Document.Type.PLAIN_TEXT).build();
104+
105+
AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document);
106+
System.out.println("response " + actualResponse.getDocumentSentiment().toString());
107+
System.exit(0);
108+
}
109+
// [END auth_cloud_api_keys]
110+
92111
public static void main(String[] args) throws IOException {
93112
if (args.length == 0) {
94113
authImplicit();
@@ -110,6 +129,15 @@ public static void main(String[] args) throws IOException {
110129
authAppEngineStandard();
111130
return;
112131
}
132+
if ("apikey".equals(args[0])) {
133+
if (args.length >= 2) {
134+
authApiKey(args[1]);
135+
} else {
136+
throw new IllegalArgumentException("Api key is required with 'apikey'.");
137+
}
138+
return;
139+
}
140+
113141
authImplicit();
114142
}
115143
}

0 commit comments

Comments
 (0)