8000 [Cloud Tasks] Add task sample with auth token (#1389) · hrodriguezv/java-docs-samples@9c54442 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c54442

Browse files
authored
[Cloud Tasks] Add task sample with auth token (GoogleCloudPlatform#1389)
* Add task sample with auth token * add parent to pom * Update comments
1 parent f8d02b4 commit 9c54442

File tree

3 files changed

+110
-12
lines changed

3 files changed

+110
-12
lines changed

tasks/README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ App Engine queues push tasks to an App Engine HTTP target. This directory
77
contains both the App Engine app to deploy, as well as the snippets to run
88
locally to push tasks to it, which could also be called on App Engine.
99

10-
`CreateTask.java` is a simple command-line program to create
11-
tasks to be pushed to the App Engine app.
12-
13-
`TaskServlet.java` is the main App Engine app. This app serves as an endpoint to receive
14-
App Engine task attempts.
10+
`CreateHTTPTask.java` constructs a task with an HTTP target and pushes it
11+
to your queue.
1512

13+
`CreateHTTPTask.java` constructs a task with an HTTP target and OIDC token and pushes it
14+
to your queue.
1615

1716
## Initial Setup
1817

@@ -28,7 +27,7 @@ App Engine task attempts.
2827
To create a queue using the Cloud SDK, use the following gcloud command:
2928

3029
```
31-
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
30+
gcloud beta tasks queues create-app-engine-queue my-queue
3231
```
3332

3433
Note: A newly created queue will route to the default App Engine service and
@@ -56,7 +55,7 @@ Then the queue ID, as specified at queue creation time. Queue IDs already
5655
created can be listed with `gcloud beta tasks queues list`.
5756

5857
```
59-
export QUEUE_ID=my-appengine-queue
58+
export QUEUE_ID=my-queue
6059
```
6160

6261
And finally the location ID, which can be discovered with
@@ -81,5 +80,15 @@ Running the sample will create a task and send the task to the specific URL
8180
endpoint, with a payload specified:
8281

8382
```
84-
mvn exec:java -Dexec.mainClass="com.example.task.CreateHttpTask"
83+
mvn exec:java@HttpTask"
84+
```
85+
86+
### Using HTTP Targets with Authentication Headers
87+
88+
In `CreateHttpTaskWithToken.java`, add your service account email in place of
89+
`<SERVICE_ACCOUNT_EMAIL>` to authenticate the OIDC token.
90+
91+
Running the sample with command:
92+
```
93+
mvn exec:java@WithToken"
8594
```

tasks/pom.xml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,28 @@ Copyright 2018 Google LLC
5454
<groupId>org.codehaus.mojo</groupId>
5555
<artifactId>exec-maven-plugin</artifactId>
5656
<version>1.4.0</version>
57-
<configuration>
58-
<mainClass>com.example.task.CreateHttpTask</mainClass>
59-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
60-
</configuration>
57+
<executions>
58+
<execution>
59+
<id>HttpTask</id>
60+
<goals>
61+
<goal>java</goal>
62+
</goals>
63+
<configuration>
64+
<mainClass>com.example.task.CreateHttpTask</mainClass>
65+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
66+
</configuration>
67+
</execution>
68+
<execution>
69+
<id>WithToken</id>
70+
<goals>
71+
<goal>java</goal>
72+
</goals>
73+
<configuration>
74+
<mainClass>com.example.task.CreateHttpTaskWithToken</mainClass>
75+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
76+
</configuration>
77+
</execution>
78+
</executions>
6179
</plugin>
6280
</plugins>
6381
</build>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.example.task;
18+
19+
// [START cloud_tasks_create_http_task_with_token]
20+
import com.google.cloud.tasks.v2beta3.CloudTasksClient;
21+
import com.google.cloud.tasks.v2beta3.HttpMethod;
22+
import com.google.cloud.tasks.v2beta3.HttpRequest;
23+
import com.google.cloud.tasks.v2beta3.OidcToken;
24+
import com.google.cloud.tasks.v2beta3.QueueName;
25+
import com.google.cloud.tasks.v2beta3.Task;
26+
import com.google.protobuf.ByteString;
27+
import java.nio.charset.Charset;
28+
29+
public class CreateHttpTaskWithToken {
30+
31+
public static void main(String[] args) throws Exception {
32+
String projectId = System.getenv("PROJECT_ID");
33+
String queueName = System.getenv("QUEUE_ID");
34+
String location = System.getenv("LOCATION_ID");
35+
String url = System.getenv("URL");
36+
37+
// Instantiates a client.
38+
try (CloudTasksClient client = CloudTasksClient.create()) {
39+
// Variables provided by the system variables.
40+
// projectId = "my-project-id";
41+
// queueName = "my-appengine-queue";
42+
// location = "us-central1";
43+
// url = "https://example.com/tasks/create";
44+
String payload = "hello";
45+
46+
// Construct the fully qualified queue name.
47+
String queuePath = QueueName.of(projectId, location, queueName).toString();
48+
49+
// Add your service account email to construct the OIDC token.
50+
// in order to add an authentication header to the request.
51+
OidcToken.Builder oidcTokenBuilder =
52+
OidcToken.newBuilder().setServiceAccountEmail("<SERVICE_ACCOUNT_EMAIL>");
53+
54+
// Construct the task body.
55+
Task.Builder taskBuilder =
56+
Task.newBuilder()
57+
.setHttpRequest(
58+
HttpRequest.newBuilder()
59+
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
60+
.setHttpMethod(HttpMethod.POST)
61+
.setUrl(url)
62+
.setOidcToken(oidcTokenBuilder)
63+
.build());
64+
65+
// Send create task request.
66+
Task task = client.createTask(queuePath, taskBuilder.build());
67+
System.out.println("Task created: " + task.getName());
68+
}
69+
}
70+
}
71+
// [END cloud_tasks_create_http_task_with_token]

0 commit comments

Comments
 (0)
0