A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://cloud.google.com/tasks/docs/creating-http-target-tasks below:

Create HTTP target tasks | Cloud Tasks Documentation

Skip to main content Create HTTP target tasks

Stay organized with collections Save and categorize content based on your preferences.

Cloud Tasks handlers can be run on any HTTP endpoint with an external IP address such as GKE, Compute Engine, or even an on-premises web server. Your tasks can be executed on any of these services in a reliable, configurable fashion.

This page demonstrates how to programmatically create basic HTTP target tasks and place them in Cloud Tasks queues. The quickstart demonstrates how to do this using the Google Cloud CLI.

For tasks that have HTTP targets (as opposed to explicit App Engine targets, which are less common), there are two ways to create tasks:

Create a task using the CreateTask method

This section discusses creating a task by constructing the task object. You use the CreateTask method.

When you create a task using the CreateTask method, you explicitly create and define the task object. You must specify the service and handler that process the task.

Optionally, you can pass task-specific data along to the handler. You can also fine-tune the configuration for the task, like scheduling a time in the future when it should be executed or limiting the number of times you want the task to be retried if it fails (see Advanced configuration).

The following examples call the CreateTask method to create a task using the Cloud Tasks client libraries.

C# Go Java

Note the pom.xml file:

Node.js

Note the package.json file:

PHP

Note the composer.json file:

Python

Note the requirements.txt file:

Ruby Create a task using the BufferTask method

This section discusses creating a task by sending an HTTP request. The method you use is called BufferTask.

Limitations

The BufferTask method is subject to the following limitations:

Call the BufferTask method

The following examples show how to create a task by sending an HTTP POST request to the Cloud Tasks API buffer endpoint.

curl

The following code snippet shows an example of task creation using with the BufferTask method using curl:

curl -X HTTP_METHOD\
"https://cloudtasks.googleapis.com/v2/projects/PROJECT_ID/locations/LOCATION/queues/QUEUE_ID/tasks:buffer" \

Replace the following:

Python Set up a service account for HTTP target handler authentication

Cloud Tasks can call HTTP target handlers that require authentication if you have a service account with the appropriate credentials to access the handler.

You can use your current service account if you grant it the appropriate roles. These instructions cover creating a new service account specifically for this function. The existing or new service account used for Cloud Tasks authentication must be in the same project as your Cloud Tasks queues.

  1. In the Google Cloud console, go to the Service Accounts page.

    Go to Service Accounts

  2. If necessary, select the appropriate project.

  3. Click Create service account.

  4. In the Service account details section, give the account a name. The console creates a related email account name for the account. This is how you reference the account. You can also add a description of what the account is for. Click Create and continue.

  5. In the Grant this service account access to project section, click Select a role. Search for and select Cloud Tasks Enqueuer. This role gives the service account permission to add tasks to the queue.

  6. Click + Add another role.

  7. Click Select a role. Search for and select Service Account User. This role allows the service account to authorize the queue to create tokens on its behalf using the service account's credentials.

  8. If your handler is part of Google Cloud, grant the service account the role associated with accessing the service where your handler is running. Each service within Google Cloud requires a different role. For example, to access a handler on Cloud Run, grant the Cloud Run Invoker role. You can use the service account you just created or any other service account in your project.

  9. Click Done to finish creating the service account.

Cloud Tasks itself must have a service account of its own that has the Cloud Tasks Service Agent role granted. This is so it can generate header tokens based on the credentials associated with the Cloud Tasks service account to authenticate with your handler target. The Cloud Tasks service account with this role granted is automatically created when you enable the Cloud Tasks API, unless you enabled it prior to March 19, 2019, in which case you must add the role manually.

Use HTTP target tasks with authentication tokens

To authenticate between Cloud Tasks and an HTTP target handler that requires such authentication, Cloud Tasks creates a header token. This token is based on the credentials in the Cloud Tasks Enqueuer service account, identified by its email address. The service account used for authentication must be part of the same project where your Cloud Tasks queue resides. The request, with the header token, is sent from the queue to the handler by HTTPS. You can use either an ID token or an access token. ID tokens should generally be used for any handler running on Google Cloud, for example, on Cloud Run functions or Cloud Run. The main exception is for Google APIs hosted on *.googleapis.com: these APIs expect an access token.

You can configure authentication at the queue level, or at the task level. To configure authentication at the queue level, see Create Cloud Tasks queues. If authentication is configured at the queue level, this configuration overrides configuration at the task level. To configure authentication at the task level, specify either an ID (OIDC) token or access (OAuth) token in the task itself.

CreateTask method

The following examples use the CreateTask method with the Cloud Tasks client libraries to create a task that also includes the creation of a header token. ID tokens are used in the examples. To use an access token, replace the OIDC parameter with the language appropriate OAuth parameter in constructing the request.

Go Java

Note the pom.xml file:

Node.js

Note the package.json file:

Python

Note the requirements.txt file:

BufferTask method

The following examples use application default credentials to authenticate when using the BufferTask method for creating a task.

curl
curl -X HTTP_METHOD\
"https://cloudtasks.googleapis.com/v2/projects/PROJECT_ID/locations/LOCATION/queues/QUEUE_ID/tasks:buffer" \
    -H "Authorization: Bearer ACCESS_TOKEN"

Replace the following:

Python

In the following code sample, provide your authentication token value.

Provide your own HTTP target task handlers

HTTP Target task handlers are very similar to App Engine task handlers, with the following exceptions:

Manually add the Cloud Tasks Service Agent role

You can manually add the Cloud Tasks Service Agent (roles/cloudtasks.serviceAgent) role to your Cloud Tasks service account which is the primary service agent for Cloud Tasks.

This is necessary only if you enabled the Cloud Tasks API prior to March 19, 2019.

Console
  1. Find and copy the project number for your Google Cloud project on the Welcome page of the Google Cloud console.
  2. In the Google Cloud console, go to the IAM page.

    Go to IAM

  3. Click Grant access. The Grant access pane opens.

  4. In the Add principals section, add an email address in this format:

     service-PROJECT_NUMBER@gcp-sa-cloudtasks.iam.gserviceaccount.com
     

    Replace PROJECT_NUMBER with your Google Cloud project number.

  5. In the Assign roles section, search for and select Cloud Tasks Service Agent.

  6. Click Save.

gcloud
  1. Find your project number:

    gcloud projects describe PROJECT_ID --format='table(projectNumber)'

    Replace PROJECT_ID with your project ID.

  2. Copy down the number.

  3. Grant the Cloud Tasks service account the Cloud Tasks Service Agent role, using the project number you copied down:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member serviceAccount:service-PROJECT_NUMBER@gcp-sa-cloudtasks.iam.gserviceaccount.com \
        --role roles/cloudtasks.serviceAgent

    Replace the following:

Note: Adding the service account can take a few minutes to complete. Other configurations

There are a number of task attributes that you can configure. For a full list, see the task resource definition. For example, you can customize the following attributes:

You can also configure queue attributes such as routing overrides, rate limits, and retry parameters. These configurations are applied to all tasks in a queue. For more information, see Configure Cloud Tasks queues.

What's next

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-07 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4