删除地区
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
用于删除区域的 Merchant API 代码示例。
Java
// Copyright 2024 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
//
// https://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 shopping.merchant.samples.accounts.regions.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.DeleteRegionRequest;
import com.google.shopping.merchant.accounts.v1.RegionName;
import com.google.shopping.merchant.accounts.v1.RegionsServiceClient;
import com.google.shopping.merchant.accounts.v1.RegionsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to delete a given region from a Merchant Center account. */
public class DeleteRegionSample {
public static void deleteRegion(Config config, String region) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
RegionsServiceSettings regionsServiceSettings =
RegionsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Calls the API and catches and prints any network failures/errors.
try (RegionsServiceClient regionsServiceClient =
RegionsServiceClient.create(regionsServiceSettings)) {
DeleteRegionRequest request = DeleteRegionRequest.newBuilder().setName(region).build();
System.out.println("Sending Delete Region request");
regionsServiceClient.deleteRegion(request); // no response returned on success
System.out.println("Delete successful.");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// Creates Region name to identify the Region you want to delete.
// Format: `accounts/{account}/region/{regionId}`
String regionId = "{INSERT_REGION_HERE}";
String region =
RegionName.newBuilder()
.setAccount(config.getAccountId().toString())
.setRegion(regionId)
.build()
.toString();
deleteRegion(config, region);
}
}
PHP
<?php
/**
* Copyright 2025 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
*
* https://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.
*/
require_once __DIR__ . '/../../../../vendor/autoload.php';
require_once __DIR__ . '/../../../Authentication/Authentication.php';
require_once __DIR__ . '/../../../Authentication/Config.php';
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\Accounts\V1\Client\RegionsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\DeleteRegionRequest;
/**
* This class demonstrates how to delete a given region from a Merchant Center account.
*/
class DeleteRegionSample
{
private static function getParent(string $accountId): string
{
return sprintf("accounts/%s", $accountId);
}
public static function deleteRegionSample(array $config, string $regionId): void
{
// Gets the OAuth credentials to make the request.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates options config containing credentials for the client to use.
$options = ['credentials' => $credentials];
$parent = self::getParent($config['accountId']);
// Creates region name to identify the region.
$name = $parent . "/regions/" . $regionId;
// Creates a client.
$regionsServiceClient = new RegionsServiceClient($options);
try {
$request = new DeleteRegionRequest([
'name' => $name
]);
print "Sending Delete Region request\n";
$regionsServiceClient->deleteRegion($request);
print "Delete successful.\n";
} catch (ApiException $e) {
print $e->getMessage();
}
}
public function callSample(): void
{
$config = Config::generateConfig();
// Replace this with the ID of the region to be deleted.
$regionId = "[INSERT REGION ID HERE]";
self::deleteRegionSample($config, $regionId);
}
}
$sample = new DeleteRegionSample();
$sample->callSample();
Python
# -*- coding: utf-8 -*-
# Copyright 2024 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.
"""A module to delete a Region."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import DeleteRegionRequest
from google.shopping.merchant_accounts_v1 import RegionsServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
def delete_region(region_id_to_delete):
"""Deletes a given region from a Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = RegionsServiceClient(credentials=credentials)
# Creates Region name to identify the Region you want to delete.
region_name = "accounts/" + _ACCOUNT + "/regions/" + region_id_to_delete
# Creates the request.
request = DeleteRegionRequest(name=region_name)
# Makes the request and catches and prints any error messages.
try:
client.delete_region(request=request)
print("Delete successful.")
except RuntimeError as e:
print("Delete failed")
print(e)
if __name__ == "__main__":
region_id = "123456AB"
delete_region(region_id)
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThis webpage provides Java and Python code samples demonstrating how to delete a region from a Merchant Center account using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe Java code sample uses the \u003ccode\u003eRegionsServiceClient\u003c/code\u003e to send a \u003ccode\u003eDeleteRegionRequest\u003c/code\u003e and delete a region specified by its name.\u003c/p\u003e\n"],["\u003cp\u003eThe Python code sample uses the \u003ccode\u003eRegionsServiceClient\u003c/code\u003e and the \u003ccode\u003eDeleteRegionRequest\u003c/code\u003e to delete a designated region.\u003c/p\u003e\n"],["\u003cp\u003eBoth code samples require OAuth credentials for authentication and use the Region's name, formatted as \u003ccode\u003eaccounts/{account}/region/{regionId}\u003c/code\u003e, to identify the region for deletion.\u003c/p\u003e\n"],["\u003cp\u003eSuccessful deletion, shown in both Java and Python examples, does not return a response but prints a success message.\u003c/p\u003e\n"]]],["The provided code demonstrates how to delete a region from a Merchant Center account using the Merchant API in Java, PHP, and Python. It involves obtaining OAuth credentials, creating a `RegionsServiceClient`, and formulating a `DeleteRegionRequest`. A region name is constructed using the account ID and region ID. The `deleteRegion` method is then invoked with this request, which sends the request to the API, deleting the specified region. Upon success, a confirmation message is printed.\n"],null,["# Delete a region\n\nMerchant API code sample to delete a region. \n\n### Java\n\n // Copyright 2024 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.accounts.regions.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1.DeleteRegionRequest;\n import com.google.shopping.merchant.accounts.v1.RegionName;\n import com.google.shopping.merchant.accounts.v1.RegionsServiceClient;\n import com.google.shopping.merchant.accounts.v1.RegionsServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to delete a given region from a Merchant Center account. */\n public class DeleteRegionSample {\n\n public static void deleteRegion(Config config, String region) throws Exception {\n\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n // Creates service settings using the credentials retrieved above.\n RegionsServiceSettings regionsServiceSettings =\n RegionsServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Calls the API and catches and prints any network failures/errors.\n try (RegionsServiceClient regionsServiceClient =\n RegionsServiceClient.create(regionsServiceSettings)) {\n DeleteRegionRequest request = DeleteRegionRequest.newBuilder().setName(region).build();\n\n System.out.println(\"Sending Delete Region request\");\n regionsServiceClient.deleteRegion(request); // no response returned on success\n System.out.println(\"Delete successful.\");\n } catch (Exception e) {\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n\n // Creates Region name to identify the Region you want to delete.\n // Format: `accounts/{account}/region/{regionId}`\n String regionId = \"{INSERT_REGION_HERE}\";\n\n String region =\n RegionName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .setRegion(regionId)\n .build()\n .toString();\n\n deleteRegion(config, region);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/regions/v1/DeleteRegionSample.java\n\n### PHP\n\n \u003c?php\n /**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n require_once __DIR__ . '/../../../../vendor/autoload.php';\n require_once __DIR__ . '/../../../Authentication/Authentication.php';\n require_once __DIR__ . '/../../../Authentication/Config.php';\n use Google\\ApiCore\\ApiException;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\RegionsServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\DeleteRegionRequest;\n\n /**\n * This class demonstrates how to delete a given region from a Merchant Center account.\n */\n class DeleteRegionSample\n {\n private static function getParent(string $accountId): string\n {\n return sprintf(\"accounts/%s\", $accountId);\n }\n public static function deleteRegionSample(array $config, string $regionId): void\n {\n // Gets the OAuth credentials to make the request.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Creates options config containing credentials for the client to use.\n $options = ['credentials' =\u003e $credentials];\n\n $parent = self::getParent($config['accountId']);\n\n // Creates region name to identify the region.\n $name = $parent . \"/regions/\" . $regionId;\n\n // Creates a client.\n $regionsServiceClient = new RegionsServiceClient($options);\n\n try {\n $request = new DeleteRegionRequest([\n 'name' =\u003e $name\n ]);\n\n print \"Sending Delete Region request\\n\";\n $regionsServiceClient-\u003edeleteRegion($request);\n print \"Delete successful.\\n\";\n } catch (ApiException $e) {\n print $e-\u003egetMessage();\n }\n }\n\n public function callSample(): void\n {\n $config = Config::generateConfig();\n\n // Replace this with the ID of the region to be deleted.\n $regionId = \"[INSERT REGION ID HERE]\";\n self::deleteRegionSample($config, $regionId);\n }\n }\n\n\n $sample = new DeleteRegionSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/regions/v1/DeleteRegionSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\n # Copyright 2024 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n \"\"\"A module to delete a Region.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import DeleteRegionRequest\n from google.shopping.merchant_accounts_v1 import RegionsServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def delete_region(region_id_to_delete):\n \"\"\"Deletes a given region from a Merchant Center account.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = RegionsServiceClient(credentials=credentials)\n\n # Creates Region name to identify the Region you want to delete.\n region_name = \"accounts/\" + _ACCOUNT + \"/regions/\" + region_id_to_delete\n\n # Creates the request.\n request = DeleteRegionRequest(name=region_name)\n\n # Makes the request and catches and prints any error messages.\n try:\n client.delete_region(request=request)\n print(\"Delete successful.\")\n except RuntimeError as e:\n print(\"Delete failed\")\n print(e)\n\n\n if __name__ == \"__main__\":\n region_id = \"123456AB\"\n delete_region(region_id)\n\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/regions/v1/delete_region_sample.py"]]