本快速入门介绍了如何使用 Cloud Tasks API 向 Cloud Tasks 队列添加 HTTP 目标任务。
准备工作
Sign in to your Google Cloud account. If you're new to
Google Cloud,
create an account to evaluate how our products perform in
real-world scenarios. New customers also get $300 in free credits to
run, test, and deploy workloads.
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Quickstart: Add an HTTP target task to a Cloud Tasks queue\n\nAdd an HTTP target task to a Cloud Tasks queue\n==============================================\n\nThis quickstart shows you how to add an\n[HTTP target task](/tasks/docs/creating-http-target-tasks) to a\nCloud Tasks queue using the Cloud Tasks API.\n\nBefore you begin\n----------------\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n- [Create or select a Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects).\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n - Create a Google Cloud project:\n\n ```\n gcloud projects create PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with a name for the Google Cloud project you are creating.\n - Select the Google Cloud project that you created:\n\n ```\n gcloud config set project PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your Google Cloud project name.\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n-\n\n\n Enable the Cloud Tasks API:\n\n\n ```bash\n gcloud services enable cloudtasks.googleapis.com\n ```\n-\n\n If you're using a local shell, then create local authentication credentials for your user\n account:\n\n ```bash\n gcloud auth application-default login\n ```\n\n You don't need to do this if you're using Cloud Shell.\n\n\n If an authentication error is returned, and you are using an external identity provider\n (IdP), confirm that you have\n [signed in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n-\n Grant roles to your user account. Run the following command once for each of the following\n IAM roles:\n `roles/cloudtasks.admin, roles/logging.viewer`\n\n ```bash\n gcloud projects add-iam-policy-binding PROJECT_ID --member=\"user:\u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e\" --role=ROLE\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: your project ID.\n - \u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e: the identifier for your user account---for example, `myemail@example.com`.\n - \u003cvar translate=\"no\"\u003eROLE\u003c/var\u003e: the IAM role that you grant to your user account.\n\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n- [Create or select a Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects).\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n - Create a Google Cloud project:\n\n ```\n gcloud projects create PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with a name for the Google Cloud project you are creating.\n - Select the Google Cloud project that you created:\n\n ```\n gcloud config set project PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your Google Cloud project name.\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n-\n\n\n Enable the Cloud Tasks API:\n\n\n ```bash\n gcloud services enable cloudtasks.googleapis.com\n ```\n-\n\n If you're using a local shell, then create local authentication credentials for your user\n account:\n\n ```bash\n gcloud auth application-default login\n ```\n\n You don't need to do this if you're using Cloud Shell.\n\n\n If an authentication error is returned, and you are using an external identity provider\n (IdP), confirm that you have\n [signed in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n-\n Grant roles to your user account. Run the following command once for each of the following\n IAM roles:\n `roles/cloudtasks.admin, roles/logging.viewer`\n\n ```bash\n gcloud projects add-iam-policy-binding PROJECT_ID --member=\"user:\u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e\" --role=ROLE\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: your project ID.\n - \u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e: the identifier for your user account---for example, `myemail@example.com`.\n - \u003cvar translate=\"no\"\u003eROLE\u003c/var\u003e: the IAM role that you grant to your user account.\n\n\u003cbr /\u003e\n\nCreate a Cloud Tasks queue\n--------------------------\n\nUse the [`gcloud tasks queues create`](/sdk/gcloud/reference/tasks/queues/create)\ncommand to create your queue.\n| **Caution:** Using Cloud Tasks `gcloud` queue management methods carries some risk if used together with methods based on uploading `queue.yaml/queue.xml`. For more information, see [Use Queue Management or queue.yaml](/tasks/docs/queue-yaml).\n\n1. In your terminal, create a queue that logs all operations.\n\n gcloud tasks queues create \u003cvar translate=\"no\"\u003eQUEUE_NAME\u003c/var\u003e \\\n --log-sampling-ratio=1.0 \\\n --location=\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eQUEUE_NAME\u003c/var\u003e: a name for your Cloud Tasks queue\n - \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e: the [supported region](/tasks/docs/locations) you deployed your service or app in\n2. Wait for the queue to initialize and then verify that it was created\n successfully.\n\n gcloud tasks queues describe \u003cvar translate=\"no\"\u003eQUEUE_NAME\u003c/var\u003e \\\n --location=\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e\n\n The output should be similar to the following: \n\n name: projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/\u003cvar translate=\"no\"\u003eLOCATION_ID\u003c/var\u003e/queues/\u003cvar translate=\"no\"\u003eQUEUE_NAME\u003c/var\u003e\n rateLimits:\n maxBurstSize: 100\n maxConcurrentDispatches: 1000\n maxDispatchesPerSecond: 500.0\n retryConfig:\n maxAttempts: 100\n maxBackoff: 3600s\n maxDoublings: 16\n minBackoff: 0.100s\n state: RUNNING\n\nAdd a task to the Cloud Tasks queue\n-----------------------------------\n\nUse the\n[`gcloud tasks create-http-task`](/sdk/gcloud/reference/tasks/create-http-task)\ncommand to create a task that targets an HTTP endpoint and add the task to your\nqueue.\n\n1. Create a task, add it to the queue you created, and deliver that task to an\n HTTP endpoint.\n\n gcloud tasks create-http-task \\\n --queue=\u003cvar translate=\"no\"\u003eQUEUE_NAME\u003c/var\u003e \\\n --url=\u003cvar translate=\"no\"\u003eURL_PATH\u003c/var\u003e \\\n --method=GET \\\n --location=\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e \\\n --project=\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003eURL_PATH\u003c/var\u003e with the full URL path that the\n request will be sent to. For example: `https://www.google.com`\n\n The path must begin with either `http://` or `https://`.\n2. Verify that the task was executed successfully by reading the logs.\n\n gcloud logging read --limit=3\n\n The logs should look similar to the following: \n\n jsonPayload:\n '@type': type.googleapis.com/google.cloud.tasks.logging.v1.TaskActivityLog\n task: projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e/queues/\u003cvar translate=\"no\"\u003eQUEUE_NAME\u003c/var\u003e/tasks/\u003cvar translate=\"no\"\u003eTASK_ID\u003c/var\u003e\n taskCreationLog:\n scheduleTime: '2024-07-04T19:00:27.801837Z'\n status: OK\n targetAddress: GET https://www.google.com/\n targetType: HTTP\n\nClean up\n--------\n\n\nTo avoid incurring charges to your Google Cloud account for\nthe resources used on this page, delete the Google Cloud project with the\nresources.\n| **Caution** : Deleting a project has the following effects:\n|\n| - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.\n| - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.\n|\n|\n| If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects\n| can help you avoid exceeding project quota limits.\n\nDelete a Google Cloud project: \n\n```\ngcloud projects delete PROJECT_ID\n```\n\nAlternatively, you can delete the Cloud Tasks queue: \n\n gcloud tasks queues delete \u003cvar translate=\"no\"\u003eQUEUE_NAME\u003c/var\u003e \\\n --location=\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e\n\nWhat's next\n-----------\n\n- [Manage queues and tasks](/tasks/docs/deleting-appengine-queues-and-tasks)\n- [Create Cloud Tasks queues](/tasks/docs/creating-queues)\n- [RPC API reference](/tasks/docs/reference/rpc)\n- [REST API reference](/tasks/docs/reference/rest)"]]