Sử dụng mã tạm thời
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Một tính năng mạnh mẽ của BatchJobService
là tính năng này hỗ trợ việc sử dụng mã nhận dạng tạm thời.
Bạn có thể làm việc này bằng cách chỉ định resource_name
của tài nguyên mới để sử dụng mã nhận dạng âm. Ví dụ: giả sử bạn tạo một chiến dịch và chỉ định tên tài nguyên của chiến dịch đó là customers/<YOUR_CUSTOMER_ID>/campaigns/-1
. Khi tạo nhóm quảng cáo trong một thao tác sau đó, bạn có thể tham chiếu nhóm quảng cáo đó theo tên tài nguyên và -1
mà bạn đã chỉ định sẽ tự động được thay thế bằng mã nhận dạng thực tế của chiến dịch đã tạo.
Sau đây là một số điều cần lưu ý khi sử dụng tên tài nguyên tạm thời:
- Bạn chỉ có thể sử dụng tên tài nguyên tạm thời sau khi tên đó được xác định trong một tài nguyên. Trong ví dụ sau, thao tác nhóm quảng cáo sẽ phải xuất hiện sau thao tác chiến dịch trong danh sách thao tác.
- Tên tài nguyên tạm thời không được lưu lại trên các yêu cầu về công việc hoặc yêu cầu biến đổi.
Để tham chiếu đến một tài nguyên được tạo trong yêu cầu về công việc hoặc yêu cầu sửa đổi trước đó, hãy sử dụng tên tài nguyên thực tế của tài nguyên đó.
- Đối với một yêu cầu đơn lẻ về công việc hoặc yêu cầu biến đổi, mỗi tên tài nguyên tạm thời phải sử dụng một số âm duy nhất, ngay cả khi chúng thuộc các loại tài nguyên khác nhau. Nếu một mã nhận dạng tạm thời được dùng lại trong một yêu cầu đơn lẻ hoặc yêu cầu đột biến, thì sẽ có lỗi trả về.
Ví dụ:
Giả sử bạn muốn thêm một chiến dịch, một nhóm quảng cáo và một quảng cáo trong một yêu cầu API duy nhất. Bạn sẽ tạo một cấu trúc cho yêu cầu của mình tương tự như sau:
mutate_operations: [
{
campaign_operation: {
create: {
resource_name: "customers/<YOUR_CUSTOMER_ID>/campaigns/-1",
...
}
}
},
{
ad_group_operation: {
create: {
resource_name: "customers/<YOUR_CUSTOMER_ID>/adGroups/-2",
campaign: "customers/<YOUR_CUSTOMER_ID>/campaigns/-1"
...
}
}
},
{
ad_group_ad_operation: {
create: {
ad_group: "customers/<YOUR_CUSTOMER_ID>/adGroups/-2"
...
}
}
},
]
Một mã tạm thời mới được dùng cho nhóm quảng cáo, vì chúng ta không thể sử dụng lại -1
mà chúng ta đã dùng cho chiến dịch. Chúng tôi cũng tham chiếu nhóm quảng cáo này khi tạo quảng cáo nhóm quảng cáo. Bản thân nhóm quảng cáo tham chiếu đến tên tài nguyên mà chúng ta đã thiết lập cho chiến dịch trong một thao tác trước đó trong yêu cầu, trong khi resource_name
trong ad_group_ad_operation
là không cần thiết vì không có thao tác nào khác tham chiếu đến nhóm quảng cáo đó.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-27 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-27 UTC."],[[["\u003cp\u003e\u003ccode\u003eBatchJobService\u003c/code\u003e allows the use of temporary IDs (negative numbers) for new resources within a single job, enabling you to reference them in subsequent operations within the same request.\u003c/p\u003e\n"],["\u003cp\u003eTemporary resource names must be unique within a single job or mutate request and are not remembered across different jobs or requests; use actual resource names for referencing resources created earlier.\u003c/p\u003e\n"],["\u003cp\u003eResources with temporary IDs must be defined before being referenced by other operations within the same request, ensuring the order of operations reflects dependencies.\u003c/p\u003e\n"]]],[],null,["# Using temporary IDs\n\nA powerful feature of [`BatchJobService`](/google-ads/api/reference/rpc/v21/BatchJobService) is that\nit supports the use of temporary IDs.\n\nYou can do this by specifying the new resource's `resource_name` to use a\nnegative ID. For example, suppose you create a campaign and specify its resource name\nas `customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1`. When you create the\nad group in a later operation, you can then reference it by that resource name\nand the `-1` you specified will be replaced by the actual ID of the created\ncampaign automatically.\n\nHere are some things to keep in mind when using temporary resource names:\n\n- A temporary resource name can only be used after it's been defined in a resource. In the following example, the ad group operation would have to appear after the campaign operation in the list of operations.\n- Temporary resource names are not remembered across jobs or mutate requests. To reference a resource created in a previous job or mutate request, use its actual resource name.\n- For a single job or mutate request, each temporary resource name must use a unique negative number, even if they are from different resource types. If a temporary ID is reused in a single job or mutate request, then an error is returned.\n\n### Example\n\nSuppose you want to add a campaign, an ad group, and an ad in a single API\nrequest. You would create a structure for your request analogous to the\nfollowing: \n\n mutate_operations: [\n {\n campaign_operation: {\n create: {\n resource_name: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1\",\n ...\n }\n }\n },\n {\n ad_group_operation: {\n create: {\n resource_name: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/adGroups/-2\",\n campaign: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/campaigns/-1\"\n ...\n }\n }\n },\n {\n ad_group_ad_operation: {\n create: {\n ad_group: \"customers/\u003cYOUR_CUSTOMER_ID\u003e/adGroups/-2\"\n ...\n }\n }\n },\n ]\n\nA new temporary ID is used for the ad group, since we can't reuse the `-1` that\nwe used for the campaign. We also reference this ad group when creating an ad\ngroup ad. The ad group itself references the resource name we established for\nthe campaign in an earlier operation in the request, while `resource_name` in\n`ad_group_ad_operation` is not necessary since no further operation is\nreferencing it."]]