-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Cloud Tasks] Add task sample with auth token #1389
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b2cbd6
Add task sample with auth token
averikitsch c6abcfa
add parent to pom
averikitsch 7631a89
Merge branch 'master' into tasksWithAuth
kurtisvg 3ed2684
Merge branch 'master' into tasksWithAuth
averikitsch 6fedeb8
Update comments
averikitsch e7aca47
Merge branch 'tasksWithAuth' of https://github.com/GoogleCloudPlatfor…
averikitsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.task; | ||
|
||
// [START cloud_tasks_create_http_task_with_token] | ||
import com.google.cloud.tasks.v2beta3.CloudTasksClient; | ||
import com.google.cloud.tasks.v2beta3.HttpMethod; | ||
import com.google.cloud.tasks.v2beta3.HttpRequest; | ||
import com.google.cloud.tasks.v2beta3.OidcToken; | ||
import com.google.cloud.tasks.v2beta3.QueueName; | ||
import com.google.cloud.tasks.v2beta3.Task; | ||
import com.google.protobuf.ByteString; | ||
import java.nio.charset.Charset; | ||
|
||
public class CreateHttpTaskWithToken { | ||
|
||
public static void main(String[] args) throws Exception { | ||
String projectId = System.getenv("PROJECT_ID"); | ||
String queueName = System.getenv("QUEUE_ID"); | ||
String location = System.getenv("LOCATION_ID"); | ||
String url = System.getenv("URL"); | ||
|
||
// Instantiates a client. | ||
try (CloudTasksClient client = CloudTasksClient.create()) { | ||
// Variables provided by the system variables. | ||
// projectId = "my-project-id"; | ||
// queueName = "my-appengine-queue"; | ||
// location = "us-central1"; | ||
// url = "https://example.com/tasks/create"; | ||
String payload = "hello"; | ||
|
||
// Construct the fully qualified queue name. | ||
String queuePath = QueueName.of(projectId, location, queueName).toString(); | ||
|
||
// Construct OIDC token | ||
OidcToken.Builder oidcTokenBuilder = | ||
OidcToken.newBuilder().setServiceAccountEmail("<SERVICE_ACCOUNT_EMAIL>"); | ||
|
||
// Construct the task body. | ||
Task.Builder taskBuilder = | ||
Task.newBuilder() | ||
.setHttpRequest( | ||
HttpRequest.newBuilder() | ||
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset())) | ||
.setHttpMethod(HttpMethod.POST) | ||
.setUrl(url) | ||
.setOidcToken(oidcTokenBuilder) | ||
.build()); | ||
|
||
// Send create task request. | ||
Task task = client.createTask(queuePath, taskBuilder.build()); | ||
System.out.println("Task created: " + task.getName()); | ||
} | ||
} | ||
} | ||
// [END cloud_tasks_create_http_task_with_token] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.