Skip to content

Commit 3b1f9c4

Browse files
averikitschchingor13
authored andcommitted
samples: [Cloud Tasks] Add task sample with auth token (#1389)
* Add task sample with auth token * add parent to pom * Update comments
1 parent 3a3c045 commit 3b1f9c4

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
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)